SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Understanding the logic with 2 returns?

    I came across a method today that has the following at the end of it:

    Code:
    return in_array($id, $arr[0]) && in_array($this->id, $arr[1]);
    Now help me understand this... That line basically says, "return TRUE if BOTH in_array functions return TRUE," correct? Otherwise, I'm assuming it would return FALSE because no other return statements exist in the method.

    Is the form that's used here done a lot? I've never seen it done this way... Usually, you only see a singular return statement.

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    4 Thread(s)
    Hi,

    Yeah you're right about what it does. As long as the expression after the return statement only evaluates to a single value then PHP won't have a problem with it.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    in_array returns a boolean -- true if the element is in in the array, false if it's not
    expression && expression "returns" (or evaluates to) a boolean -- true if both expressions are true, false if either or both expressions are false

    So in total the function returns a boolean -- true if both elements are in their respective arrays, or false if either or both elements aren't in their respective arrays.

    This form isn't used a whole lot, but you do see it every once in a while.

    Another thing I'm reminded off by looking at this is that the following also confuses people a lot

    Code:
    class Something
    {
        public $somevar;
        
        public function foo()
        {
            return $this->somevar = 5;
        }
    }
    Which
    • returns 5, AND
    • sets $this->somevar to 5


    at the same time (compare to if (($pos = strpos($haystack, $needle)) !== false) which can be similarly confusing)

    For a complete list of logical operators like && see http://php.net/manual/en/language.operators.logical.php
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    for readability, i'd have wrapped it in a set of parenthesis (just to avoid potential confusion of precedence; does it evaluate as (return function1()) && function2() or return (function1() && function2()) ), but yeah...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •