php帮助调用通过return语句使用的变量

<!doctype html>
<html>
<head>
</head>
<body>
<?php
function distance($x,$y){
distance(10,10);
$d = $x * $y;
return $d;
echo $d; //not working
}
echo $d;
if ($d > 5){ //notworking
echo "grats";
}
?>
</body>
</html>

my code on line 14 and 11 is not working, line 11 wont output $d. line 14 wont call upon returned value $d.

It wont work, becase you haven't called the function. Use this instead:

<?php
    function distance($x,$y)
    {
        return ($x * $y);
    }

    $d = distance(10, 10);

    if ($d > 5) { 
        echo "grats";
    }
?>

The return statement leaves the function. Use

  • echo distance(); to display the function return value or

  • $d = distance(); for assigning the value to a var

This may help in understanding return: http://www.php.net/manual/en/function.return.php

You have multiple problems.

return $d;
echo $d; < not working

The above does not work because when you call return in a function the function immediately stops executing. Any code after the return statement will not execute.

distance(10,10);

The above line does nothing in our function except call your function recursively forever. I don't know why your code does not throw an error for this. You can safely remove this as it does nothing constructive.

if ($d > 5){ <not working

The above code does not work because you never call your function to populate $d with a value. That line needs to look like this:

$d = distance(10, 10);
if ($d > 5){