-5

好的,所以我有一些适合我的代码。

调用函数d($z);

$z 本身是代码中其他地方的声明变量。

这不起作用:

function d($x1){
$distance = $x1;
return $distance;
}

这有效:

function d($x1){
$distance = $x1;
echo $distance;
}

任何想法为什么?

注意:我不希望值回显。我希望以后能够将该值用作变量,而不必打印到屏幕上。(现在,上面是一个简化版本,已经删除了很多我真正想做的事情,但即使是上面的内容也不能为我正确返回)

虽然这不是我想要做的,因为我想使用 $distance 的结果作为内部变量(而不是回显它),如果我这样做<?=$distance?>它不适用于返回。

4

2 回答 2

1

有 2 个可能(可能更多)的问题:

  1. 您没有将正确的值传递给函数

    或者

  2. 您没有将返回分配给变量(例如$value = d("some value here");

编辑:不要忘记回显 $value (存储返回)

于 2013-07-05T23:02:08.823 回答
0

Both are working exactly as expected.

In first example you don't display the value of function d

change it to

function d($x1)
{
         $distance = $x1;
         return $distance;
}

echo d(5);

and it will display it.

In second case you don't return the value so function is void, but you display the distance inside the function d() so it's displayed.

于 2013-07-05T23:35:31.623 回答