我一直收到错误:解析错误:语法错误,意外的'sName'(T_STRING),在第5行期待']'我需要一些帮助来解决这个问题。谁能告诉我我的代码有什么问题?
<!DOCTYPE html>
<?php
$data = array [
["sName” => “Gervase”, "sNumber" => "s1234567", "mark” => 95, “comments” => “Well done!”],
["sName” => “Matt”, "sNumber" => "s1234567", "mark” => 95, “comments” => “Well done!”],
["sName” => “Kid”, "sNumber" => "s1234567", "mark” => 95, “comments” => “Well done!”],
["sName” => “Nathan”, "sNumber" => "s1234567", "mark” => 85, “comments” => “Well done!”]
]
?>
<html>
<head>
<link type='text/css' rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
</head>
<body>
<div>
<div class="jumbotron">
<div>
<div class="container">
<h1>Data</h1>
</div>
<div>
<div class="container">
<table>
<table class="table">
<thead>
<?php
foreach ($data as $data)
{
echo"<tr>";
echo"<td>".$data["sName"]."</td>";
echo"<td>".$data["sNumber"]."</td>";
echo"</tr>";
}
?>
<tr>
<th>Student number</th>
<th>Student name</th>
<th>Mark</th>
<th>Comments</th>
</tr>
<tr>
<td>Something</td>
</tr>
</thead>
</table>
</div<
</div>
</body>
答案 0 :(得分:1)
“我一直收到错误:解析错误:语法错误,意外的'sName'(T_STRING),在第5行期待']'
你看到这些卷曲的引号? “ ”
。
您需要将它们全部更改为常规引号"
。
["sName" => "Gervase", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Matt", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Kid", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Nathan", "sNumber" => "s1234567", "mark" => 85, "comments" => "Well done!"]
您可能已经使用某种“Word”处理器进行了编码,或者将其从某个地方的网络中删除,但没有正确转换这些引号。最好使用代码“编辑器”。
另外,如果你的PHP版本是&lt; 5.4,您需要将代码更改为array()
。但是,鉴于您发布的错误消息,此处似乎并非如此。否则,您将获得Parse error: syntax error, unexpected '[', expecting '('
。
即:
$data = array (
["sName" => "Gervase", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Matt", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Kid", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Nathan", "sNumber" => "s1234567", "mark" => 85, "comments" => "Well done!"]
)
以防万一,如果你在同一个PHP块中的那个块之后有任何PHP,则添加一个结束分号
"comments" => "Well done!"]
);
<强>脚注:强>
看到你只有2个<td>...</td>
缺少元素,请添加另外两个:
echo "<td>".$data["mark"]."</td>";
echo "<td>".$data["comments"]."</td>";
这是一个额外的奖励。
我觉得很奇怪但是你的数据下面有列标题。如果你想要它们(我认为会更好),请把它放在上面。
您在这里的关闭错误</div<
应该是</div>
和迷路<table>
标记,并且会在调试/ HTML源代码中生成错误。
即:
<div class="container">
<table class="table">
<thead>
<tr>
<th>Student number</th>
<th>Student name</th>
<th>Mark</th>
<th>Comments</th>
</tr>
<?php
foreach ($data as $data)
{
echo"<tr>";
echo"<td>".$data["sName"]."</td>";
echo"<td>".$data["sNumber"]."</td>";
echo"<td>".$data["mark"]."</td>";
echo"<td>".$data["comments"]."</td>";
echo"</tr>";
}
?>
<tr>
<td>Something</td>
</tr>
</thead>
</table>
</div>
你还有一些关闭</div>
的关闭,但这超出了这个问题的范围。你需要匹配它们。
答案 1 :(得分:0)
鉴于当前代码,它可能与$ data数组的右括号后缺少的分号有关。
除此之外,看起来你的代码混合了代表引号的不同字符。您可以根据问题块中应用的格式颜色来查看此内容。尝试直接在编辑器中重新键入数组(而不是复制粘贴或其他内容),以便所有字符都匹配,看看是否有帮助。
答案 2 :(得分:0)
复制并粘贴以下代码,有一些微妙的错误,例如array[]
它应该是array()
,foreach($data as $data)
它应该像foreach($data as $datum)
$ datum可以是任何东西但不是$数据,最重要的是你的文本编辑器出错了....
看看显示颜色,即使是这段代码,您可能会遇到不同的颜色来呈现$data
,请使用其他文本编辑器,因为您当前的文本编辑器将您的常规报价更改为其他内容所以....
<!DOCTYPE html>
<?php
$data = array(
["sName"=>"Gervase", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"],
["sName"=>"Matt", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"],
["sName"=>"Kid", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"],
["sName"=>"Nathan", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"]
);
?>
<html>
<head>
<link type='text/css' rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
</head>
<body>
<div>
<div class="jumbotron">
<div>
<div class="container">
<h1>Data</h1>
</div>
<div>
<div class="container">
<table>
<table class="table">
<thead>
<tr>
<th>Student number</th>
<th>Student name</th>
<th>Mark</th>
<th>Comments</th>
</tr>
</tr>
</thead>
<tbody>
<?php
foreach ($data as $datum)
{
echo"<tr>".
"<td>".$datum["sName"]."</td>".
"<td>".$datum["sNumber"]."</td>".
"<td>".$datum["mark"]."</td>".
"<td>".$datum["comment"]."</td>".
"</tr>";
}
?>
<tr>
<td>Something</td>
</tr>
</thead>
</table>
</div<
</div>
</body>