我制作了一个php脚本,您必须在其中添加内容到存储桶列表中,但是它无法正常工作。该脚本需要输出存储区列表,但只输出最后输入的值。
<?php
echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";
if (is_numeric($a)) {
for ($x = 1; $x <= $a; $x++) {
echo $d;
$c = readline();
}
} else {
echo "'$a' isn't a number, try again.";
exit;
}
$bucketlist = [$c];
array_push($bucketlist);
foreach ($bucketlist as $z) {
echo "On your bucket list:\n";
echo $z;
echo "\n";
}
?>
我需要它来输出在readline中输入的所有内容 $c
正如上述作者所提到的,它$c = readline();
发生在循环内,因此每次迭代都将其覆盖。尝试以下方法:
echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";
$bucketlist = [];
if (is_numeric($a)) {
for ($x = 1; $x <= $a; $x++) {
echo $d;
$c = readline();
array_push($bucketlist, $c);
}
} else {
echo "'$a' isn't a number, try again.";
exit;
}
foreach ($bucketlist as $z) {
echo "On your bucket list:\n";
echo $z;
echo "\n";
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句