This question already has an answer here:
I keep getting the error: Parse error: syntax error, unexpected 'sNameâ€' (T_STRING), expecting ']'on line 5 I need some help solving this problem. Could anyone tell me what's wrong with my code?
<!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>
</div>
Given the current code, it may be related to the missing semi-colon after the closing bracket of the $data array.
Beyond that, it looks like your code is mixing different characters that represent quotation marks. You can see this based on the formatting colors SO applied in your question block. Try re-typing the array directly in your editor (rather than copy-and-pasting or something) so that all the characters match up, and see if that helps.
"I keep getting the error: Parse error: syntax error, unexpected 'sNameâ€' (T_STRING), expecting ']'on line 5"
You see these curly quotes? “ ”
.
You need to change them all to regular quotes "
.
["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!"]
You may have coded with some kind of "Word" processor or pulled it off the web somewhere, that didn't convert those quotes properly. It's best to use a code "editor".
Plus, if your PHP version is < 5.4, you will need to change your code to array()
. However, this doesn't seem to be the case here, given the error message you posted. Otherwise, you would be getting Parse error: syntax error, unexpected '[', expecting '('
instead.
I.e.:
$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!"]
)
and just in case, add a closing semi-colon should you have any PHP following that block inside the same PHP block
"comments" => "Well done!"]
);
Footnotes:
Seeing you only have 2 <td>...</td>
with missing elements, add the other two:
echo "<td>".$data["mark"]."</td>";
echo "<td>".$data["comments"]."</td>";
This being a bonus add-on.
I find it strange though in having the column headers below your data. If you want them on top (which by my opinion would be better), put it above it.
You also have the wrong closing here </div<
which should have been </div>
and a stray <table>
tag, and would have generated an error in debug/HTML source.
I.e.:
<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>
You also have a few missing closing </div>
's in there, but that's beyond the scope of this question. You need to match those up.
Copy and paste following code, there were subtle error like array[]
it should be array()
, foreach($data as $data)
it should be like foreach($data as $datum)
$datum can be anything but not $data, and the most important thing is something error with your text editor to.... Look at the display color even this code you may experience different color for presenting $data
, please use some other text editor, because your current text editor changes your regular quote to something else so....
<!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>