1

I have the following code:

$SpeedA = 5; 
$SpeedB = 5; 
$Distance = 20; 

function CalDistance ($SpeedA, $SpeedB, 
$Distance)
{
    $DistanceA = (($SpeedA / $SpeedB) * 
    $Distance) / (1 + ($SpeedA / $SpeedB));

    Return $DistanceA;

}
echo $DistanceA;

I get this error:

Notice: undefined variable $DistanceA

Why $DistanceA is regarded as undefined and how to fix it?

1
  • Side note: I'd of ordinarily have closed this as an existing undefined index/variable duplicate, but I applaud your use of the "undefined-variable" tag. You can consult the answer that was given below; it seems to be correct to me. Commented Oct 1, 2018 at 1:23

1 Answer 1

2

You never call the function CalDistance, before your echo. So, you try to echo an undefined $DistanceA.

So, you could do something like this:

$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;
Sign up to request clarification or add additional context in comments.

2 Comments

I see. That makes sense. I'm new and some times I just look at it too long and can see the wood for the trees. Thanks!
Your welcome. If it solved your problem, then please accept the answer. So, that future readers, see that your problem was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.