PHP Switch Statement


  1. What will be the output of the following PHP code ?
    <?php
    $n = 128;
    switch($n)
    {
    case "n":
    print "Welcome to";
    break;
    case 128:
    print "Interview";
    break;
    default:
    print "Mania";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Downcasting does not happen in case,it compares only with its data type.


  1. What will be the output of the following PHP code ?
    <?php
    $p = "1";
    $p = 1;
    $q = 1;
    switch($p)
    {
    case $p * $q:
    print "Hello";
    break;
    case $p / $q:
    print "Hey";
    break;
    default:
    print "Bye";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    It checks the first case, when it finds it equal it will perform it breaks out.



  1. What will be the output of the following PHP code ?
    <?php
    $opt = "1";
    switch($opt)
    {
    case 1:
    break;
    print "First Statement";
    case 2:
    print "Second Statement";
    break;
    default:
    print "Third Statement";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    As break is provided before print statement in case 1 it breaks the loop before printing.


  1. What will be the output of the following PHP code ?
    <?php
    $opt = "1";
    switch ($opt)
    {
    case 1:
    print "Hello ";
    case 2:
    print "Interview ";
    default:
    print "Mania ";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    As break is not provided it executes all the cases.