PHP find output programs (Loops) | set 1

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.
Submitted by Nidhi, on January 20, 2021

Question 1:

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

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

Question 2:

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

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

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.

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

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.

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

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.

Question 5:

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

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.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.