PHP find output programs (conditional statements) | set 1

Find the output of PHP programs | Conditional Statements | Set 1: Enhance the knowledge of PHP conditional statements by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 17, 2021

Question 1:

<?php
    $PI = 3.14;
    
    if ($PI == PI())
    {
        echo "Matched";
    }
    else
    {
        echo "$PI is not Matched with " . PI();
    }
?>

Output:

3.14 is not Matched with 3.1415926535898

Explanation:

In the above program, we used if...else statements, Here, we defined a variable $PI with value 3.14, and checked equality condition with the value returned by PI() function. But PI() function returns value 3.1415926535898, which is different from 3.14 then the condition gets false.

Question 2:

<?php
    $A = 20;
    $B = 12;
    
    $A = $A * 12 + 5;
    $B = 20 * ($B / 2 * 2) + 5;
    
    if ($A == $B)
    {
        echo "www.includehelp.com";
    }
    else
    {
        echo "www.google.com";
    }
?>

Output:

www.includehelp.com

Explanation:

In the above program, we created two variables $A and $B initialized with 20 and 12 respectively.

$A = $A*12+5;
$B = 20*($B/2*2)+5;

Now evaluate the above expressions:

$A = $A*12+5;
$A = 20*12+5;
$A = 240+5;
$A = 245;

And,

$B = 20*($B/2*2)+5;
$B = 20*(12/2*2)+5;
$B = 20*(6*2)+5;
$B = 20*12+5;
$B = 240+5;
$B = 245;

Then the value of $A and $B will be the same, so the condition gets true and "www.includehelp.com" will be printed on the console screen.

Question 3:

<?php
    $NUM1 = intval(PI());
    $NUM2 = (81 / pow(3, 3));
    
    if ($NUM1 === $NUM2)
    {
        echo "www.includehelp.com";
    }
    else
    {
        echo "www.google.com";
    }
?>

Output:

www.includehelp.com

Explanation:

In the above program, we created two variables $NUM1 and $NUM2.

$NUM1 =  intval(PI()); 
$NUM2 =  (81/pow(3,3));

Let's evaluate the above expression:

$NUM1 =  intval(PI()); 
$NUM1 =  intval(3.1415926535898);
$NUM1 =  3;

And then,

$NUM2 = (81/pow(3,3));
$NUM2 = (81/27));
$NUM2 = 3;

Now condition if ($NUM1===$NUM2) will be true, and then "www.includehelp.com" will print on the webpage.

Question 4:

<?php
    if (print (print ("Hello World ")))
    {
        echo "www.includehelp.com";
    }
    else
    {
        echo "www.google.com";
    }
?>

Output:

Hello World 1www.includehelp.com

Explanation:

In the above program, we used nested print() function. The print() function returns 1 on successful printing, otherwise, it returns false.

Let's evaluate the below statement:

if (print(print("Hello World "))) 

The inner print() will print "Hello World " and return 1, then the outer print() function will print 1 and return 1 then the condition will true and print "www.includehelp.com" on the webpage.

Question 5:

<?php
    $A = 10;
    $B = 20;
    
    $R = printf("Hello : %d,%d ", $A, $B);
    
    if ($R > 20)
    {
        echo "www.includehelp.com";
    }
    else
    {
        echo "www.google.com";
    }
?>

Output:

Hello : 10,20 www.google.com

Explanation:

In the above program, we defined two variables $A and $B initialized with 10 and 20 respectively. Here, we used the printf() function, which is used for print formatted output. The printf() returns the total number of characters printed on the webpage.

$R = printf("Hello : %d,%d ",$A, $B);

In the above statement will print "Hello : 10,20 " on the webpage and return 14, which is assigned to variable $R. Then the if condition gets false, and "www.google.com" will be printed on the webpage.





Comments and Discussions!

Load comments ↻





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