+ 2

not to sure what has happened here and why it is not working could use a little help please

I am working on php practitioner: step 7: Associative arrays. There error is: Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH) in /Users/dylanmckinstry/Php Practice/index-tmp.php on line 25 The first file of code is: For the index.php. <?php $person= [ 'age' => 40, 'hair' => 'brown', 'career' => 'Help desk support' ]; require 'index-tmp.php'; ?> The second file index-tmp.php which is house the html code. <!Doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Practice</title> <style media="screen"> header{ background: #e3e3e3; padding: 2em; text-align: center; } </style> </head> <body> <header> <h1> <ul> <?php foreach ($person as $feature) : ?> <li><?= $feature; ?></li> <? php endforeach; ?> </ul> </h1> </header> </body> </html>

2nd Jun 2018, 4:10 PM
Dylan McKinstry
Dylan McKinstry - avatar
6 Antworten
+ 5
In php you need to use array() function
2nd Jun 2018, 4:31 PM
Toni Isotalo
Toni Isotalo - avatar
+ 3
$person = array(”a” => ”b”)
2nd Jun 2018, 4:33 PM
Toni Isotalo
Toni Isotalo - avatar
+ 1
try the following: ... <ul> <?php foreach ($person as $feature) : echo "<li>$feature</li>"; endforeach; ?> </ul> ...
2nd Jun 2018, 6:59 PM
Johann
Johann - avatar
+ 1
Toni Isotalo that's the old way. Using square brackets works just as fine.
4th Jun 2018, 1:15 AM
Parker McMullin
Parker McMullin - avatar
0
This makes no sense: <?php foreach ($person as $feature) : ?> <li><?= $feature; ?></li> <? php endforeach; ?> Output <li>...</li> as a whole, keep things within one <?php ... ?>, don't split php code like this...
2nd Jun 2018, 4:19 PM
Jiří Bočan
0
I think I found the issue. There's a space between <? and php on the endforeach line.
4th Jun 2018, 1:59 AM
Parker McMullin
Parker McMullin - avatar