PHP find output programs (Loops) | set 2

Find the output of PHP programs | Loops | Set 2: 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
    $x = 1;
    $NUM = 5;

    while ($x <= strlen("ABCDEFGHIJ"))
    {
        printf("%dX%d = %d", $NUM, $x, $NUM * $x);
        echo '<br>';
        $x++;
    }
?>

Output:

5X1 = 5
5X2 = 10
5X3 = 15
5X4 = 20
5X5 = 25
5X6 = 30
5X7 = 35
5X8 = 40
5X9 = 45
5X10 = 50

Explanation:

In the above program, we created two variables $x and $NUM initialized with 1 and 5 respectively. Here, we used while loop, in the condition of while loop, strlen() function is used.

The strlen() returns the length of the specified string. the strlen("ABCDEFGHIJ") will return 10. So that condition of while loop will be true until the value of $x is less than or equal to 10.

The value of $x increase from 1 to 10, when it becomes 11 then the loop will terminate. We used the printf() function inside the body of the loop to print values in each iteration and the value of $x will increase in each iteration.

Question 2:

<?php
    $RES = 1;
    $NUM = printf("Hello");
    
    while ($NUM > 0)
    {
        $RES = $RES * $NUM;
        $NUM--;
    }
    
    echo "<BR>$RES";
?>

Output:

Hello
120

Explanation:

In the above program, we created $RES with initial value 1 and created one more variable $NUM, which is initialized with the value returned by printf() function. The printf() function will return the total number of characters printed on the webpage.

Here, $NUM will be initialized with 5.

Here, loop condition will true until $num becomes 0. In the body of the loop value of the $NUM will decrease in each iteration.

$NUM=5, $RES=1
	$RES = 1 * 5;
	$RES = 5;
Then $NUM will be 4 after decrement.

$NUM=4, $RES=5
	$RES = 5 * 4;
	$RES = 20;
Then $NUM will be 3 after decrement.

$NUM=3, $RES=20
	$RES = 20 * 3;
	$RES = 60;
Then $NUM will be 2 after decrement.

$NUM=2, $RES=60
	$RES = 60 * 2;
	$RES = 120;
Then $NUM will be 1 after decrement.

$NUM=1, $RES=60
	$RES = 120 * 1;
	$RES = 120;

Then $NUM will be 0 after decrement. And loop condition will false and print 120 on the webpage.

Question 3:

<?php
    $RES = 1;
    $NUM = pow(2, 2) + print ("Hello");
    
    while ($NUM > 0)
    {
        $RES = $RES * $NUM;
        $NUM--;
    }
    echo "<BR>$RES";
?>

Output:

Hello
120

Explanation:

In the above program, we created $RES with initial value 1 and created one more variable $NUM, which is initialized below expression.

Th pow() function will return 4 and print() function will return 1. Because print() function will return 1 on successful printing of data.

Here, $NUM will be initialized with 5.

Here, loop condition will true until $num becomes 0. In the body of the loop value of $NUM will decrease in each iteration.

$NUM=5, $RES=1
	$RES = 1 * 5;
	$RES = 5;
Then $NUM will be 4 after decrement.

$NUM=4, $RES=5
	$RES = 5 * 4;
	$RES = 20;
Then $NUM will be 3 after decrement.

$NUM=3, $RES=20
	$RES = 20 * 3;
	$RES = 60;
Then $NUM will be 2 after decrement.

$NUM=2, $RES=60
	$RES = 60 * 2;
	$RES = 120;
Then $NUM will be 1 after decrement.

$NUM=1, $RES=60
	$RES = 120 * 1;
	$RES = 120;

Then $NUM will be 0 after decrement. And loop condition will false and print 120 on the webpage.

Question 4:

<?php
    $RES = 1;
    $NUM = (1, 2, 3, 4, 5);
    
    while ($NUM > 0)
    {
        $RES = $RES * $NUM;
        $NUM--;
    }
    echo "<BR>$RES";
?>

Output:

PHP Parse error:  syntax error, unexpected ',' in /home/main.php on line 3

Explanation:

The above program will generate a compilation error because of the below statement.

$NUM = (1,2,3,4,5);

We cannot initialize values to $NUM like this.

Question 5:

<?php
    $RES = 1;
    
    for ($I = 1;$I <= 5;$I++)
    {
        $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 and loop condition will true until it becomes more than 5.

$I=1, $RES=1 condition will true
	$RES = $RES + $I;
	$RES = 1 + 1;
	$RES = 2;
$I=2, $RES=2 condition will true
	$RES = $RES + $I;
	$RES = 2 + 2;
	$RES = 4;
$I=3, $RES=4 condition will true
	$RES = $RES + $I;
	$RES = 4 + 3;
	$RES = 7;
$I=4, $RES=7 condition will true
	$RES = $RES + $I;
	$RES = 7 + 4;
	$RES = 11;
$I=5, $RES=11 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.





Comments and Discussions!

Load comments ↻





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