php 函数中定义的变量出现未定义变量错误
Getting an undefined variable error for variable defined within function in php
我有以下代码:
$SpeedA = 5;
$SpeedB = 5;
$Distance = 20;
function CalDistance ($SpeedA, $SpeedB,
$Distance)
{
$DistanceA = (($SpeedA / $SpeedB) *
$Distance) / (1 + ($SpeedA / $SpeedB));
Return $DistanceA;
}
echo $DistanceA;
我收到这个错误:
Notice: undefined variable $DistanceA
为什么 $DistanceA
被视为未定义以及如何解决?
您永远不会在 echo
之前调用函数 CalDistance
。所以,你尝试 echo
一个未定义的 $DistanceA
.
所以,你可以这样做:
$SpeedA = 5;
$SpeedB = 5;
$Distance = 20;
function CalDistance ($SpeedA, $SpeedB,
$Distance)
{
$DistanceA = (($SpeedA / $SpeedB) *
$Distance) / (1 + ($SpeedA / $SpeedB));
Return $DistanceA;
}
$call = CalDistance($SpeedA, $SpeedB, $Distance);
echo $call;
我有以下代码:
$SpeedA = 5;
$SpeedB = 5;
$Distance = 20;
function CalDistance ($SpeedA, $SpeedB,
$Distance)
{
$DistanceA = (($SpeedA / $SpeedB) *
$Distance) / (1 + ($SpeedA / $SpeedB));
Return $DistanceA;
}
echo $DistanceA;
我收到这个错误:
Notice: undefined variable $DistanceA
为什么 $DistanceA
被视为未定义以及如何解决?
您永远不会在 echo
之前调用函数 CalDistance
。所以,你尝试 echo
一个未定义的 $DistanceA
.
所以,你可以这样做:
$SpeedA = 5;
$SpeedB = 5;
$Distance = 20;
function CalDistance ($SpeedA, $SpeedB,
$Distance)
{
$DistanceA = (($SpeedA / $SpeedB) *
$Distance) / (1 + ($SpeedA / $SpeedB));
Return $DistanceA;
}
$call = CalDistance($SpeedA, $SpeedB, $Distance);
echo $call;