Thread: Evaluating &&

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189

    Evaluating &&


    This should be such a basic question, but I've never known the answer, and wondered many times..

    With && in an IF statement, do all portions get evaluated, or do they get evaluated in order, and it stops when it hits a false?

    Example:

    PHP Code:
    if (is_array($something) && array_count_values($something)>30) { 
    Would evaluation of this statement halt after is_array() if "$something" was not an array, or would it continue and evaluate the next part too?

    Reason for question, is if array_count_values() was a function that will error if the value sent to it is not an array (and I do NOT know if this is the case for this function) then it would be better to nest the IF statements.

    PHP Code:
    if (is_array($something)) {
       if (
    array_count_values($something)) {
          
    //More here
       
    }

    Thomas Tremain
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    Minutes later I found my own answer...

    "This type of evaluation is called short-circuiting. Once the result is 100% clear, it does not continue evaluating."

    http://stackoverflow.com/questions/2456086/if-with-multiple-conditions-order-of-execution
    Thomas Tremain
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    whenever you encounter questions like this: Just try it out. Make a function, put it after "false &&" and see for yourself if it gets called.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo