谁能修复我的 foreach 循环?
我有这样的代码
<?php
$array = array('http://www.google.com', 'http://www.facebook.com', 'http://www.twitter.com');
foreach ( $array as $website );
{
echo "<pre>";
echo "Hi $website\n";
echo "Hello $website\n";
echo "Welcome $website\n";
echo "</pre>";
}
?>
我的输出看起来像这样
Hi http://www.twitter.com
Hello http://www.twitter.com
Welcome http://www.twitter.com
我的意思是我的代码仅打印最后一个数组值。但我想要这样的输出。
Hi http://www.google.com
Hello http://www.google.com
Welcome http://www.google.com
Hi http://www.facebook.com
Hello http://www.facebook.com
Welcome http://www.facebook.com
Hi http://www.twitter.com
Hello http://www.twitter.com
Welcome http://www.twitter.com
谁能告诉我为什么它只打印最后一个值? 谢谢
I have code like this
<?php
$array = array('http://www.google.com', 'http://www.facebook.com', 'http://www.twitter.com');
foreach ( $array as $website );
{
echo "<pre>";
echo "Hi $website\n";
echo "Hello $website\n";
echo "Welcome $website\n";
echo "</pre>";
}
?>
My output looks like this
Hi http://www.twitter.com
Hello http://www.twitter.com
Welcome http://www.twitter.com
I mean my code printing only the last array value. But i want output like this.
Hi http://www.google.com
Hello http://www.google.com
Welcome http://www.google.com
Hi http://www.facebook.com
Hello http://www.facebook.com
Welcome http://www.facebook.com
Hi http://www.twitter.com
Hello http://www.twitter.com
Welcome http://www.twitter.com
Can anyone tell me why its only printing last value?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
删除分号:
Remove the semi-colon:
该行
有一个额外的
;
发生的情况是,您有一个空的 foreach,然后当
$website
位于最后一项时,您执行一个带有 echo 的块。删除
;
就可以了This line
has an extra
;
What happens is that you have an empty foreach, and then while
$website
is on the last item, you perform a block with an echo.remove the
;
and you'll be fine从此行中删除分号:
Remove the semicolon from this line:
foreach 循环末尾有一个分号 (;)。这意味着 { } 块不是在循环内部,而是在 foreach 循环完成迭代后执行一次。
只需删除 ;从最后。
You have a semicolon (;) at the end of your foreach loop. This means that instead of the { } block being inside the loop, it just executes once, after the foreach loop finished iterating.
Just drop the ; from the end.
foreach
语句末尾有一个分号。删除是,您将拥有您需要的功能You have a semicolon at the end of your
foreach
statement. Remove is and you will have the functionality you require