Register|Login

Have a question? Click here to ask the community.

Quiz 18 – Foreach Loop

| In: PHP Quizzes | 12,653 views

*You are currently not logged in and your quiz score will not be recorded. Please login or register if you would like to submit your quiz score.

Directions: Answer all questions. Any questions skipped will be scored as wrong answers. Good Luck! :)

1. A foreach loop contains an internal pointer that points to an index (or element) in an array and assigns the value of that element to a variable?

True
False

2. A foreach loop can only be used for arrays?

True
False

3. What will be displayed in a browser when the following PHP code is executed:

<?php
   $numbers = array(9, 2, 1);
   foreach ($numbers as $value){
       echo "$value ";
   } 
?>

9 1 2
9 2
9 2 1
1

4. What will be displayed in a browser when the following PHP code is executed:

<?php
   $numAndNames = array(9, "Robert", 1, "Julia");
   foreach ($numAndNames as $content){
       echo "$content ";
   } 
?>

Julia
Robert 1 Julia
9
9 Robert 1 Julia

5. What will be displayed in a browser when the following PHP code is executed:
Note: This may be a tough one for new PHP programmers but just think about it.

<?php
   $numbers[0] = 8;
   $numbers[1] = 99;
   $numbers[2] = 100;
   $numbers[3] = -2;
   $numbers[4] = 9000;
   foreach ($numbers as $value){
       echo "$value ";
   } 
?>

9000
8 99 100 -2
9000 -2 100 99 8
8 99 100 -2 9000

Comment Form

You must be logged in to post a comment.

Users Online

Users Online: 3 Users

Most Users Ever Online: 91

Quiz Scores

Advertisments