I am learning to build a whole simple website in PHP, but some of the code has an error. I have partially fixed it and I am hoping to learn from fixing the error on the older code which is PHP 5+.
The original offending code was this, the echo line, just one line, which cause a parsing error on firefox
<?php
foreach ($navItems as $item) {
echo "<li><a href=\"$item['slug']\">$item['title']</a></li>";
}
?>
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
I tried to improve it with this
<?php
foreach ($navItems as $item) {
echo '<li><a href="$item' . '["slug"]">$item["title"]</a></li>';
}
?>
But now it 'echo' out incorrectly with - $item["title"] - for each menu item
If the correction I made is correct then I will have to drop the course as it will be too difficult to correct all, if you can help with this small problem i would be very grateful