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
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;
Edited by Psycho
Link to comment
Share on other sites

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>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.