Jump to content

What is wrong with my code?


sittiponder

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/282227-what-is-wrong-with-my-code/
Share on other sites

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;

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>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.