PHP find output programs (switch Statement) | set 3

Find the output of PHP programs | switch Statement | Set 3: 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 = 3;
    
    switch ($var)
    {
        case 0to1:
            echo "India";
        break;
    
        case 2to3:
            echo "Australia";
        break;
    
        case 4to5:
            echo "USA";
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

PHP Parse error:  syntax error, unexpected 'to1' (T_STRING) 
in /home/main.php on line 6

Explanation:

The above program will generate syntax error because we cannot define cases in switch block like this.

Question 2:

<?php
    $var = 3;
    
    switch ($var)
    {
        case 4 - 1:
            echo "India";
        break;
    
        case 9 - 5:
            echo "Australia";
        break;
    
        case 16 - 11:
            echo "USA";
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

India

Explanation:

The above program will print "India" on the webpage. In the above program "case" within switch block contain expressions, after evaluating expression switch block will be like this:

switch ($var) 
{
    case 3:
        echo "India";
    break;
        
    case 4:
        echo "Australia";
    break;
        
    case 5:
        echo "USA";  
    break;
        
    default:
        echo "Wrong Choice";
    break;
}

Then "case 3" will execute and print "India" on the webpage.

Question 3:

<?php
    switch (printf("Hello"))
    {
        case 4 - 1:
            echo "India";
            switch ('1')
            {
                case 1:
                    echo "MP";
                break;
    
                default:
                    echo "UP";
                break;
            }
        break;
    
        case 9 - 5:
            echo "Australia";
            switch ('1')
            {
                case 1:
                    echo "MP";
                break;
                default:
                    echo "UP";
                break;
            }
        break;
    
        case 16 - 11:
            echo "USA";
            switch ('1')
            {
                case 1:
                    echo "MP";
                break;
                default:
                    echo "UP";
                break;
            }
        break;
    
        default:
            echo "Wrong Choice";
        break;
    }
?>

Output:

HelloUSAMP

Explanation:

The above program will print "HelloUSAMP" on the webpage. In the above program print() function used in outer switch will print "Hello" and return 5, then  "case 16 - 11:" will execute and print "USA" and execute inner switch that will print "MP" on the webpage.

Question 4:

<?php
    switch (3.14)
    {
        case "3.14":
            echo "Australia";
        break;
    
        case 3.14:
            echo "India";
        break;
    
        case '3.14':
            echo "USA";
        break;
    }
?>

Output:

Australia

Explanation:

The program will print "Australia" on the webpage. In PHP 3.14, "3.14", '3.14' are equal, here we used all in switch case. In the situation, the first case in the switch block will execute and print "Australia" on the webpage.

Question 5:

<?php
    $var = "3" . ".14";
    
    switch (3.14)
    {
        case $var:
            echo "Australia";
        break;
    
        case 3.14:
            echo "India";
        break;
    
        case '3.14':
            echo "USA";
        break;
    }
?>

Output:

Australia

Explanation:

The program will print "Australia" on the webpage. In PHP 3.14, "3.14", '3.14' are equal, in the above program we created a variable $var, which is initialized with ("3".".14"), here we concatenated the "3" with ".14" then the final value of $var will be "3.14". Then "case $var:" will execute and print "Australia" on the webpage.





Comments and Discussions!

Load comments ↻





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