PHP If Else/Else If Statement


  1. What will be the output of the following PHP code ?
    <?php
    if (!print "First")
    if (print "Second")
    print "Third";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Print returns true and thus the first if statement also is not executed.


  1. What will be the output of the following PHP code ?
    <?php
    if (print "Ninth" - 9)
    print "Tenth"
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    print returns true and when 9 is subtracted it is syntax error.



  1. What will be the output of the following PHP code ?
    <?php
    $num = 1;
    if ($num--)
    print "First"
    $num--;
    else
    print "Second"
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    The if statement has no brackets and it expects a else/else if after a if.


  1. What will be the output of the following PHP code ?
    <?php
    $p = 25;
    $q = 26;
    if ($p < ++$p || $q < ++$q)
    print "if statement executed...";
    else
    print "else statement executed...";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The operator precedence of ++ is higher than <,thus the increment happens first and then compared.



  1. What will be the output of the following PHP code ?
    <?php
    $n = 12;
    if ($n-- - --$n - $n)
    print "First";
    else
    print "Second";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Computing the expression in the if clause,it sums upto to 2 which is a positive value.