PHP Tutorial

PHP Examples

PHP Practice

PHP find output programs (switch Statement) | set 1

Find the output of PHP programs | switch Statement | Set 1: Enhance the knowledge of PHP switch Statement concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 22, 2021

Question 1:

<?php
    $var = "1";
    
    switch ($var)
    {
        case 1:
            echo "One";
        break;
    
        case 2:
            echo "Two";
        break;
    
        case 3:
            echo "Three";
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

One

Explanation:

In the above program, we used switch-case statements. Here, we created a variable $var initialized with "1". Switch block contains multiple cases, the $var will match with "case 1" because 1 and "1" can be matched in switch case block in PHP. Then finally "One" will print on the webpage.

Question 2:

<?php
    $var = print ("OK ");
    
    switch ($var)
    {
        case 1:
            echo "One";
        break;
    
        case 2:
            echo "Two";
        break;
    
        case 3:
            echo "Three";
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

OK One

Explanation:

In the above program, we used switch-case statements. Here, we created a variable $var, which is initialized with the value returned by the print() function. The print() function returns 1 on successful printing data specified in it. So the print() function will print "OK " and return 1. Then the value of $var will be 1.

The switch block contains multiple cases, the $var will match with "case 1" and print "One" on the webpage.

Question 3:

<?php
    $var = printf("OK ");
    
    switch ($var)
    {
        case 1:
            echo "One";
        break;
    
        case 2:
            echo "Two";
        break;
    
        case 3:
            echo "Three";
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

OK Three

Explanation:

In the above program, we used switch-case statements. Here, we created a variable $var, which is initialized with the value returned by the printf() function. The printf() function return the total number of characters printed by the printf(), here "OK " are 3 characters, then it will return 3. So printf() function will print "OK " and return 3. Then the value of $var will be 3.

The switch block contains multiple cases, the $var will match with "case 3" and print "Three" on the webpage.

Question 4:

<?php
    $var = true;
    
    switch ($var)
    {
        case 1:
            echo "One";
        break;
    
        case 2:
            echo "Two";
        break;
    
        case 3:
            echo "Three";
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

One

Explanation:

In the above program, we used switch-case statements. Here, we created a variable $var and initialized with true, "true" is a predefined constant that's value is 1. So, $var is actually initialized with 1. Then $var will match with "case 1" and print "One" on the webpage.

Question 5:

<?php
    $var = true + true;
    
    switch ($var)
    {
        case 1:
            echo "One";
        break;
        case 2:
            echo "Two";
        case 3:
            echo "Three";
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

TwoThree

Explanation:

In the above program, we used switch-case statements. Here, we created a variable $var and initialized with (true+true), "true" is a predefined constant that's value is 1. So, $var is actually initialized with 2. Then $var will match with "case 2" and print "Two", but here we missed break statement after "case 2" then it will also execute "case 3" and also print "Three" on the webpage.

Then the final output "TwoThree" will be printed on the webpage.





Comments and Discussions!

Load comments ↻





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