PHP If Else/Else If Statement


  1. What will be the output of the following PHP code ?
    <?php
    $n = 5;
    if ($n++)
    print "Hello Interview Mania";
    else
    print "Hey Interview Mania";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    n is incremented after if which evaluates to false.


  1. What will be the output of the following PHP code?
    <?php
    $str;
    if ($str)
    print "Interview" ;
    else
    print "Mania";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Uninitialized str is set to 0, thus if condition fails.



  1. What will be the output of the following PHP code ?
    <?php
    $p = "0";
    $q = "1";
    if ((int)$p && $q)
    print"Interview";
    else
    print "Mania";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The expression is evaluated with values contained in the string, even without typecasting.


  1. What will be the output of the following PHP code ?
    <?php
    if (print "Program ")
    print "Executed..." ;
    else
    print "Not Executed...";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    Expression evaluates to true as print returns 1.



  1. What will be the output of the following PHP code ?
    <?php
    $num = 5;
    if ($num == 6)
    print "Interveiw" ;
    else if($num = 6)
    print $num;
    else
    print "Mania";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Enters if else as first condition is false and thus num is set to 6.