Q:

PHP find output programs (Loops) | set 1

0

Find the output of PHP programs | Loops | Set 1: Enhance the knowledge of PHP Loops concepts by solving and finding the output of some PHP programs.

Question 1:

<?php
    $ARR = array(
        "SUN",
        "MON",
        "TUE",
        "WED"
    );
    
    foreach ($VALin $ARR) echo "$VAL <br>";
?>

Question 2:

<?php
$ARR = array(
    "SUN",
    "MON",
    "TUE",
    10
);

foreach ($ARR as $VAL) echo "$VAL <br>";
?>

Question 3:

<?php
    $ARR = array(
        "SUN",
        "MON",
        "TUE",
        10
    );
    
    foreach ($ARR as $VAL)
    {
        if ($VAL == 10) break;
        $i = 0;
        while ($i < 3)
        {
            echo $VAL[$i++];
        }
        echo '<br>';
    }
?>

Question 4:

<?php
    $ARR = array(
        "SUN",
        10,
        "MON",
        "TUE",
        10
    );
    
    foreach ($ARR as $VAL)
    {
        if ($VAL == 10) continue;
        $i = 0;
        while ($i < 3)
        {
            echo $VAL[$i++];
        }
        echo '<br>';
    }
?>

Question 5:

<?php
    $ARR = array(
        "SUN",
        '10',
        "MON",
        "TUE",
        10
    );
    
    foreach ($ARR as $VAL)
    {
        $i = 0;
        while ($i < 3)
        {
            echo $VAL[$i++];
        }
        echo '<br>';
    }
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

PHP Parse error:  syntax error, unexpected '$ARR' (T_VARIABLE) in 
/home/main.php on line 9

Explanation:

The above program will generate compilation error because we did not use proper syntax of foreach loop. To access the elements of array, we need to use as keyword. The correct syntax is given below:

foreach ($ARR as $VAL) 
    echo "$VAL <br>"; 

Answer 2:

Output:

SUN
MON
TUE
10

Explanation:

In the above program, we created an array $ARR that contain values "SUN", "MON", "TUE", 10. Here, we access values from array one by one using foreach loop and print on the webpage using the echo statement.

Answer 3:

Output:

SUN
MON
TUE

Explanation:

In the above program, we created an array $ARR that contain values "SUN", "MON", "TUE", 10.

Here, we access values from array one by one using foreach loop.

In the body of foreach loop, we used condition if($VAL==10) to terminate the loop, when the value of $VAL will be 10 then the loop will terminate.

Here we also used nested while loop that will access characters by one by from the string and print on the webpage.

Answer 4:

Output:

SUN
MON
TUE

Explanation:

In the above program, we created an array $ARR that contain values "SUN", 10, "MON", "TUE", 10.

Here, we access values from array one by one using the foreach loop.

In the body of foreach loop, we used condition if($VAL==10) to skip some values, when the value of $VAL will be 10 then it will skip the execution of the loop body and move to the next element of the array.

Here, we also used nested while loop that will access character by one by from the string and print on the webpage.

Answer 5:

Output:

SUN
10
MON
TUE

Explanation:

In the above program, we created an array $ARR that contains values "SUN", '10', "MON", "TUE", 10.

Here, we access values from array one by one using the foreach loop.

Here, we also used nested while loop that will access character by one by from the string and print on the webpage.

Using the nested loop, we cannot access integer value 10 then it will not print on the webpage.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now