Q:

PHP find output programs (switch Statement) | set 1

0

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.

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;
    }
?>

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;
    }
?>

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;
    }
?>

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;
    }
?>

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;
    }
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

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.

Answer 2:

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.

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now