Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting PHP syntax error unexpected 'foreach' (T_FOREACH)

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.

<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>";
    }
}
?>
like image 475
Andre Ellis Avatar asked Dec 03 '14 02:12

Andre Ellis


1 Answers

Usually when PHP returns "unexpected" it means the thing before it is not quite right, as with this case:

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 ;

And looks like you've copy/pasted the mistake (missing semi colon) to other places in your code too

like image 155
James Avatar answered Oct 30 '22 17:10

James