sittiponder Posted September 17, 2013 Share Posted September 17, 2013 This is for a class on codecademy. I'm getting a parse error for an unexpected T_variable on line 35. <html> <head> <title>Iteration Nation</title> </head> <body> <p> <?php $food = array('pizza', 'salad', 'burger'); $salad = array('lettuce' => 'with', 'tomato' => 'without', 'onions' => 'with'); // Looping through an array using "for". // First, let's get the length of the array! $length = count($food); // Remember, arrays in PHP are zero-based: for ($i = 0; $i < $length; $i++) { echo $food[$i] . '<br />'; } echo '<br /><br />I want my salad:<br />'; // Loop through an associative array using "foreach": foreach ($salad as $ingredient=>$include) { echo $include . ' ' . $ingredient . '<br />'; } echo '<br /><br />'; // Create your own array here and loop // through it using foreach! $candy = array('chocolate'=> 'M&Ms', 'chocolate'=>'Snickers', 'chocolate'=>'Reese\'s', 'gummy'=>'bears', 'gummy'=>'worms'); foreach ($candy as $type => $specific); if $type == 'chocolate'{ echo $specific; } elseif { echo $type . ' ' . $specific; } else { echo 'Unknown candy'; } ?> </p> </body> </html> I'm sure it's a simple mistake. I'm new to coding. Quote Link to comment https://forums.phpfreaks.com/topic/282227-what-is-wrong-with-my-code/ Share on other sites More sharing options...
Psycho Posted September 17, 2013 Share Posted September 17, 2013 (edited) There should not be a semi-colon at the end of the foreach() condition. foreach ($candy as $type => $specific); The code that the foreach should run against should either be encloised in curly braces or use the alternative syntax of starting with a colon and ending with an end statement foreach ($candy as $type => $specific) { if $type == 'chocolate'{ echo $specific; } elseif { echo $type . ' ' . $specific; } else { echo 'Unknown candy'; } } foreach ($candy as $type => $specific): if $type == 'chocolate'{ echo $specific; } elseif { echo $type . ' ' . $specific; } else { echo 'Unknown candy'; } endforeach; Edited September 17, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/282227-what-is-wrong-with-my-code/#findComment-1449936 Share on other sites More sharing options...
gizmola Posted September 17, 2013 Share Posted September 17, 2013 Need a condition on that elseif () there Psycho, and the first condition needs a () around it Quote Link to comment https://forums.phpfreaks.com/topic/282227-what-is-wrong-with-my-code/#findComment-1449939 Share on other sites More sharing options...
jcbones Posted September 17, 2013 Share Posted September 17, 2013 As well as this array: $candy = array('chocolate'=> 'M&Ms', 'chocolate'=>'Snickers', 'chocolate'=>'Reese\'s', 'gummy'=>'bears', 'gummy'=>'worms'); Doesn't contain what you think it does. Dump it and see. echo '<pre>' . print_r($candy,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/282227-what-is-wrong-with-my-code/#findComment-1449946 Share on other sites More sharing options...
Psycho Posted September 18, 2013 Share Posted September 18, 2013 Need a condition on that elseif () there Psycho, and the first condition needs a () around it Yeah, I just saw the problem with the container for the code block and corrected that without seeing any of the other problems. Quote Link to comment https://forums.phpfreaks.com/topic/282227-what-is-wrong-with-my-code/#findComment-1449978 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.