PHP find output programs (Loops) | set 3

Find the output of PHP programs | Loops | Set 3: Enhance the knowledge of PHP Loops concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 21, 2021

Question 1:

<?php
    $RES = 1;
    
    for ($I = 1;;$I++)
    {
        $RES = $RES + $I;
    }
    
    echo "$RES";
?>

Output:

INFINITE LOOP.

Explanation:

In the above program situation of the infinite loop will occur because here we did not put any condition for the loop. If we did not mention the loop condition then the loop will condition is always true, and the loop will never terminate.

Question 2:

<?php
    $RES = 1;
    
    for ($I = 1;;$I++)
    {
        if ($I > 5) break;
        $RES = $RES + $I;
    }
    
    echo "$RES";
?>

Output:

16

Explanation:

The above program will print "16" on the webpage. In the above program, we created $RES initialized with 1. Here, we used counter variable $I, which is initialized with 1.

Here, we did not put any condition for the loop. If we did not mention the loop condition then the loop will condition is always true. But for loop termination, we used if condition and loop will terminate using the break statement.

$I=1, $RES=1 and IF condition will true
	$RES = $RES + $I;
	$RES = 1 + 1;
	$RES = 2;
$I=2, $RES=2 and IF condition will true
	$RES = $RES + $I;
	$RES = 2 + 2;
	$RES = 4;
$I=3, $RES=4 and IF condition will true
	$RES = $RES + $I;
	$RES = 4 + 3;
	$RES = 7;
$I=4, $RES=7 and IF condition will true
	$RES = $RES + $I;
	$RES = 7 + 4;
	$RES = 11;
$I=5, $RES=11 and IF condition will true
	$RES = $RES + $I;
	$RES = 11 + 5;
	$RES = 16;

$I=6, $RES=16 condition will false and 16 will print on the webpage.

Question 3:

<?php
    $RES = 1;
    for ($I = 1;$RES < ((int)(pi()));$I++);
    {
        $RES = $RES + $I;
    }
    
    echo "$RES";
?>

Output:

INFINITE LOOP.

Explanation:

In the above program situation of the infinite loop will occur because we used semicolon at the end of the loop. Then the value of $RES will not increase and the loop condition will never false.

Question 4:

<?php
    $RES = 1;
    
    for ($I = 1;$I < ((int)(pi()));$I++)
    {
        $RES = $RES + $I;
    }
    
    echo "$RES";
?>

Output:

4

Explanation:

In the above program, we created $RES and initialized it with 1. And we used the below expression in place of loop condition.

((int)(pi()))

This expression will return 3 for the loop condition.

$I=1, $RES=1 and condition will true
	$RES = $RES + $I;
	$RES = 1 + 1;
	$RES = 2;
$I=2, $RES=2 and condition will true
	$RES = $RES + $I;
	$RES = 2 + 2;
	$RES = 4;
$I=3, $RES=4 then the condition will false.

Question 5:

<?php
    $VAR = "HELLO";
    $I = 0;
    
    do
    {
        echo $VAR[$I++] . '<BR>';
    }
    while ($I < 5);
?>

Output:

H
E
L
L
O

Explanation:

In the above program, we declared a variable $VAR and $I that are initialized with "HELLO" and 0 respectively. Here, we used "do …while" loop and access characters one by one form the string "HELLO" and print them on the webpage until $I becomes 5.





Comments and Discussions!

Load comments ↻





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