Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Flaws of "and" and "or" operators in PHP

    • 0
    • 2
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 109
    Comment on it

    Today, When I was doing some programming stuffs and I found out that there is serious flaw in the and , or logical operator of PHP.

    Im not talking about the symbol || and && logical operator. Im talking about the and and or logical operator (operator with words).

    Example of the flaw of using and and or logical operator

    Look at the following example and notice the output of this example,

    $return_val = false or true;
    var_dump($return_val); //prints bool(false)
    $return_val = true and false;
    var_dump($return_val); //prints bool(true)
    

    As you can see in the first example, bool(false) is the output in the browser. The "true or false" always produces a "true" and there is no doubt about this. But look at the output, what a freaking output by the or operator of PHP.

    And now, just look at the second example, youll see bool(true) as a output to the browser. What a ridiculous result? How can true and false can be true, it must be false without any doubt. But, the symbolic "||" and "&&" produces the right answer. So I would recommend you to use symbolic logical operators.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: