查看变量和函数时出错
error in viewing variable and function
我有这个代码:
<?php
function random(){
echo rand(0,50);
};
for (
$x = 1;
$x <= 20;
$x++){
echo $x." : ".random()."<br>";
};
?>
这是一些输出
231 :
232 :
93 :
84 :
15 :
应该是:
1 : 23
2 : 23
3 : 9
4 : 8
5 : 1
这是因为echo rand(0,50);
。你需要使用 return rand(0,50);
,见下文:-
<?php
function random(){
return rand(0,50);
};
for (
$x = 1;
$x <= 20;
$x++){
echo $x." : ".random()."<br>";
};
?>
注意:- @Mark Baker 给出了正确的解释:-
echo rand(0,50);
将在串联之前执行,母鸡将首先获得其输出,然后是 $x
值。因此,让您的随机函数 return 可以连接到循环内的 echo 语句中的值
我有这个代码:
<?php
function random(){
echo rand(0,50);
};
for (
$x = 1;
$x <= 20;
$x++){
echo $x." : ".random()."<br>";
};
?>
这是一些输出
231 :
232 :
93 :
84 :
15 :
应该是:
1 : 23
2 : 23
3 : 9
4 : 8
5 : 1
这是因为echo rand(0,50);
。你需要使用 return rand(0,50);
,见下文:-
<?php
function random(){
return rand(0,50);
};
for (
$x = 1;
$x <= 20;
$x++){
echo $x." : ".random()."<br>";
};
?>
注意:- @Mark Baker 给出了正确的解释:-
echo rand(0,50);
将在串联之前执行,母鸡将首先获得其输出,然后是 $x
值。因此,让您的随机函数 return 可以连接到循环内的 echo 语句中的值