Simulating Foreach

Objectives:
  • To predict output in sight reading only
  • To challenge existing knowledge in PHP

For this exercise, predict the output of given codes without seeking the console first. This skill is a must to learn when aiming to be a developer.

  1. What do you think will be the output of the code below? Try to guess before running it!

    $list = array(2,4,6,8);
    foreach($list as $key => $value)
    {
     echo $key . " - " . $value ."<br />";
    }
    
  2. What would be the output of the following code? Try to guess before running it!

    $list = array(2,4,6,8);
    foreach($list as $value)
    {
     echo $value ."<br />";
    }
    
  3. What will be the output of this? Again, guess the output before running the code.

    $fruits= array("A" => "Apple", "B" => "Banana");
    foreach($fruits as $key => $value)
    {
     echo $key . " - " . $value ."<br />";
    }
    
  4. How about the following code? Try to guess the output of the code before running it!

    $fruits= array("A" => "Apple", "B" => "Banana");
    foreach($fruits as $key => $value)
    {
     echo $value ."<br />";
    }
    
  5. Let's change the echo statement. Make a guess!

    $fruits= array("A" => "Apple", "B" => "Banana");
    foreach($fruits as $key => $value)
    {
     echo $key ."<br />";
    }
    
  6. Okay. Now let's make it more challenging. What would be the output of the following code? ``` $plots = array( array("a1", "a2",...

Please submit your assignment before moving to the next lesson. chevron_left Prev Nextchevron_right