PHP Switch Statement


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Case cannot be defined by a variable.


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Error Undefined variable: t



  1. What will be the output of the following PHP code ?
    <?php
    $k = 1;
    switch(print $k)
    {
    case 2:
    print "Statement1";
    break;
    case 1:
    print "Statement2";
    break;
    default:
    print "Statement3";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    Print returns 1,thus it gives case 1.


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Constants cannot be used in switch cases.



  1. What will be the output of the following PHP code ?
    <?php
    $s = 2.2;
    switch($s)
    {
    case 2.1:
    print "Option A";
    break;
    case 2.2:
    print "Option B";
    break;
    default:
    print "Option C";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    it compares it with 2.2 and thus prints Option B and exits.