PHP Operators


  1. What will be the output of the following PHP code ?
    <?php
    $num = 8;
    while (--$num > 0 || ++$num)
    {
    print $num;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    As it is || operator the second expression is not evaluated till num becomes 1 then it goes into a loop.


  1. What will be the output of the following PHP code ?
    <?php
    $p = 2;
    while ((--$p > ++$p) - 2)
    {
    print $p;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    (–$p > ++$p) evaluates to 2 but -2 makes it enters the loop and prints p which is 2.



  1. What will be the output of the following PHP code ?
    <?php
    $k = -1;
    while (++$k && --$k)
    {
    print $k;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The first condition itself fails thus the loop exits.


  1. What will be the output of the following PHP code ?
    <?php
    $p = 0;
    while(++$p || --$p)
    {
    print $p;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    As it is || operator the second expression is not evaluated and p is always incremented, in the first case to 1.



  1. What will be the output of the following PHP code ?
    <?php
    $n = 4;
    while (++$n)
    {
    while ($n --> 0)
    print $n;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The loop ends when n becomes 0.