<?php
function iterate($x){ //this is the function
for ($i = 0; $i <= 10; $i++){ //this is the loop procedure to iterate 10 times
echo $i; //it will show 0123456789 on the screen
}
}
$y = "xyz"; //variable declaration
echo iterate($y); //should be iterate xyz as much 10 times.
?>
希望在php函数内使用for循环回显(打印)xyz十次,结果不如预期。 如何显示xyz迭代十次。
答案 0 :(得分:4)
echo $x;
这是您传递给函数的值。您不必回显函数,因为函数内部会调用echo。你还需要改变你的柜台。 0到9是10次,或1到10.
function iterate($x){ //this is the function
for ($i = 0; $i <= 9; $i++){ //this is the loop procedure to iterate 10 times
echo $x; //it will show xyz on the screen, 10 times
}
}
$y = "xyz"; //variable declaration
iterate($y); //should be iterate xyz as much 10 times.
答案 1 :(得分:0)
看起来你对你正在做的事感到困惑。
您正在打印用于迭代的$ i而不是您传递的那个(i,例如$x
)
解决这个问题,
你应该回复$x
这是你要打印的那个
<?php
function iterate($x){ //this is the function
for ($i = 0; $i <= 10; $i++){ //this is the loop procedure to iterate 10 times
echo $x; //it will show 0123456789 on the screen
}
}
?>
既然逻辑已修复,那么在这里仍然存在一些问题,你打印的函数是打印xyz。
<?php
$y = "xyz"; //variable declaration
iterate($y); //should be iterate xyz as much 10 times.
?>
结合两种解决方案:
<?php
function iterate($x){ //this is the function
for ($i = 0; $i <= 10; $i++){ //this is the loop procedure to iterate 10 times
echo $x; //it will show 0123456789 on the screen
}
}
$y = "xyz"; //variable declaration
iterate($y); //should be iterate xyz as much 10 times.
?>
答案 2 :(得分:-1)
如果我理解你的问题
for ($i = 0; $i <= 10; $i++){
echo $y; //instead of xyz
}
答案 3 :(得分:-2)
<?php
function iterate($x){ //this is the function
for ($i = 0; $i < 10; $i++){ //this is the loop procedure to iterate 10 times
echo "{$x}<br/>"
}
}
$y = "xyz"; //variable declaration
echo iterate($y); //should be iterate xyz as much 10 times.