获取 PHP 语法错误意外的“foreach”(T_FOREACH)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27262558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting PHP syntax error unexpected 'foreach' (T_FOREACH)
提问by Andre Ellis
I am trying to make a foreach code that automatically makes my header for my site. The idea is that as I add more pages I can just add them to the array and it will auto update the header for me. Usually my foreach'es work but for the first time I am having trouble. I tried it two ways, both of which spat out the same error.
我正在尝试制作一个 foreach 代码,它会自动为我的网站制作标题。这个想法是,当我添加更多页面时,我可以将它们添加到数组中,它会自动为我更新标题。通常我的 foreach'es 工作,但我第一次遇到麻烦。我尝试了两种方法,这两种方法都出现了相同的错误。
<link rel="stylesheet" type="text/css" href="css/header.css" />
<?php
$page = array("index.php", "about.php");
$names = array("Home", "About")
foreach($names as $name){
echo "<a href=$page> $name </a>";
}
?>
<link rel="stylesheet" type="text/css" href="css/header.css" />
<?php
$page = array("index.php", "about.php");
$names = array("Home", "About")
foreach($page as $pages){
foreach($names as $name){
echo "<a href=$pages> $name </a>";
}
}
?>
回答by James
Usually when PHP returns "unexpected" it means the thing before it is not quite right, as with this case:
通常,当 PHP 返回“意外”时,它意味着之前的事情不太正确,就像这种情况:
You have $names = array("Home", "About")
<-- no semi colon to end the line, and so the next line foreach
is unexpected, as it was "expecting" a ;
你有$names = array("Home", "About")
<-- 没有分号来结束这一行,所以下一行foreach
是意外的,因为它“期待”一个;
And looks like you've copy/pasted the mistake (missing semi colon) to other places in your code too
看起来您也将错误(缺少分号)复制/粘贴到代码中的其他位置
回答by user889030
when php return error "unexpected" it line # xxx , then it mean you missing semi colon ";" it line # xxx-1 mean above line of xxx line
当 php 返回错误“意外”时,它是第 # xxx 行,则意味着您缺少分号“;” it line # xxx-1 表示 xxx 行的上方
so in your case your are missing semi color ";" above foreach loop
所以在你的情况下你缺少半色“;” 在 foreach 循环之上
$names = array("Home", "About")
回答by Mikel Bitson
Try this, with comments to explain:
试试这个,用注释来解释:
<head>
<link rel="stylesheet" type="text/css" href="css/header.css" />
</head>
<body>
<?php
$page = array("index.php", "about.php");
$names = array("Home", "About"); // Fixed semicolon
// Added the key before the foreach. This allows us to reference which number in we are.
foreach($names as $key => $name)
{
// Added the key to the page array variable to specify the right page.
echo "<a href=".$page[$key].">".$name."</a>";
}?>
</body>
To further simplify using array keys, try this:
要进一步简化使用数组键,请尝试以下操作:
<?php
// Define page names as key and url as value.
$pages = array("Home" => "index.php", "About" => "about.php");
// Added the key before the foreach as url this time
foreach($pages as $name => $url)
{
// Added the key to the page array variable to specify the right page.
echo "<a href=".$url.">".$name."</a>";
}?>