PHP Tutorial

PHP Examples

PHP Practice

PHP find output programs (conditional statements) | set 2

Find the output of PHP programs | Conditional Statements | Set 2: 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
    $A = 10;
    $B = 20;
    
    $R = printf("Hello : %d %d", $A, printf("%d ", $B));
    
    if ($R > 13)
    {
        echo "www.includehelp.com";
    }
    else
    {
        echo "www.google.com";
    }
?>

Output:

20 Hello : 10 3www.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, printf("%d ",$B));

In the above statement, inner printf() will print "20" on the webpage and return 3, then outer printf() will print "Hello : 10 3" and return 12, which is assigned to variable $R. Then the if condition gets false, and "www.google.com" will be printed on the webpage.

Question 2:

<?php
    $A = 10;
    $B = 20;
    $C = 30;
    
    if ($A > $B && $A > $C)
    {
        echo "Hello World";
    }
    else if ($B > $A && $B > $C)
    {
        echo "Hello India";
    }
    else
    {
        echo "Hello Delhi";
    }
?>

Output:

Hello Delhi

Explanation:

In the above program, we created three variables $A, $B, and $C initialized with 10, 20, and 30 respectively.

First of all, condition if($A>$B && $A>$C) will check, here, 10 is less than 20 and 30, then condition get false. After that else if($B>$A && $B>$C) will check, here condition also get false. Then finally else part will execute and print "Hello Delhi" on the webpage.

Question 3:

<?php
    $A = 10;
    $B = 20;
    $C = 30;
    
    if ($A > $B and $A > $C)
    {
        echo "Hello World";
    }
    else if ($B > $A and $B > $C)
    {
        echo "Hello India";
    }
    else
    {
        echo "Hello Delhi";
    }
?>

Output:

Hello Delhi

Explanation:

In the above program, we created three variables $A, $B, and $C initialized with 10, 20, and 30 respectively.

Note: In the PHP && or and both are used to check conditions.

First of all, condition if($A>$B and $A>$C) will check, here 10 is less than 20 and 30, then condition get false. After that else if($B>$A and $B>$C) will check, here condition also gets false. Then finally else part will execute and print "Hello Delhi" on the webpage.

Question 4:

<?php
    $A = 10;
    $B = 20;
    $C = 30;
    
    if ($A > $B)
    {
        if ($A > $C) echo "Hello World";
        else echo "Hello india";
    }
    else if ($B > $A)
    {
        if ($B > $C) echo "Hiii World";
        else echo "Hiii india";
    }
    else
    {
        echo "Namaste Universe";
    }
?>

Output:

Hiii india

Explanation:

In the above program, we created three variables $A, $B, and $C initialized with 10, 20, and 30 respectively.

Here, condition if($A>$B) will false, and else if($B>$A) condition will true, but inner condition if($B>$C) will false then "Hiii india" will be printed on the webpage.

Question 5:

<?php
    $num = printf("%02d", printf("%03d", 20));
    
    if ($num % 2 == 0) echo "EVEN";
    else echo "ODD";
?>

Output:

02003EVEN

Explanation:

In the above program, we used nested printf() function, the inner printf() function will print "020" and return 3, then outer printf() will print "03" and return 2, which is assigned to $num.

Now if ($num%2==0) will true, then the message "EVEN" will print on the webpage.





Comments and Discussions!

Load comments ↻





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