implicit if() ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lawrence Krubner

    #1

    implicit if() ?


    What is this syntax?

    $hasMaxRecordLi mit = ($this->getMaxRecordLi mit() !== false);


    What is happening here? Is this some sort of implicit if statement? As
    in "Assign the value of getMaxRecordLim it if it does not equal false"?

    I noticed this line here:


  • ursynek

    #2
    Re: implicit if() ?

    Lawrence Krubner pisze:
    >
    What is this syntax?
    >
    $hasMaxRecordLi mit = ($this->getMaxRecordLi mit() !== false);
    >
    >
    What is happening here? Is this some sort of implicit if statement? As
    in "Assign the value of getMaxRecordLim it if it does not equal false"?
    >
    I noticed this line here:
    >

    >

    $a !== $b

    Not identical TRUE if $a is not equal to $b, or they are not
    of the same type. (introduced in PHP 4)

    I suppose that function $this->getMaxRecordLi mit() return true
    or false. So $hasMaxRecordLi mit will be 0 when
    $this->getMaxRecordLi mit() return false, otherwise will be 1.

    =]

    Sorry for my poor english :P

    Comment

    • Erwin Moller

      #3
      Re: implicit if() ?


      Lawrence Krubner schreef:
      >
      What is this syntax?
      >
      $hasMaxRecordLi mit = ($this->getMaxRecordLi mit() !== false);
      >
      >
      What is happening here? Is this some sort of implicit if statement? As
      in "Assign the value of getMaxRecordLim it if it does not equal false"?
      Hi,

      $hasMaxRecordLi mit gets the value of the expression after the =.
      So it will be whatever this evaluates to:
      ($this->getMaxRecordLi mit() !== false)

      Since the used expression can only evaluate to true or false,
      $hasMaxRecordLi mit will be also be true or false.

      Now we have to guess what $this->getMaxRecordLi mit() returns.
      I *guess* this method returns a integer if MaxRecordLimit is set/valid.
      And $this->getMaxRecordLi mit() returns false if MaxRecordLimit is not
      set/valid.

      If my assumptions are correct, you'll get one of the following situations:
      a) $this->getMaxRecordLi mit() returns a number (say 550):
      $hasMaxRecordLi mit = (550 !== false);
      which evaluates to true (since 550 is indeed not equal to false)

      b) $this->getMaxRecordLi mit() returns false
      $hasMaxRecordLi mit = (false !== false);
      which is false (since false is equal to false)

      If you wonder why is says !== instead of !=, the reason is simple:
      !== is more strict than != because it also checks type of the used
      variables.

      In case of $this->getMaxRecordLi mit() returns the number 0 (instead of
      my used 550) it makes a diffence, because 0 evaluates to false. That is
      probably not what the designer wanted, since 0 is a valid number in this
      situation.

      So:
      (0 != false) <-- false
      (0 !== false) <-- true

      Regards,
      Erwin Moller

      --
      =============== =============
      Erwin Moller
      Now dropping all postings from googlegroups.
      Why? http://improve-usenet.org/
      =============== =============

      Comment

      • Erwin Moller

        #4
        Re: implicit if() ?


        Erwin Moller schreef:
        >
        Lawrence Krubner schreef:
        >>
        >What is this syntax?
        >>
        >$hasMaxRecordL imit = ($this->getMaxRecordLi mit() !== false);
        Hi,

        I forgot to mention:

        I think this is equivalent:
        $hasMaxRecordLi mit = is_numeric($thi s->getMaxRecordLi mit());
        which is much clearer.

        Regards,
        Erwin Moller

        --
        =============== =============
        Erwin Moller
        Now dropping all postings from googlegroups.
        Why? http://improve-usenet.org/
        =============== =============

        Comment

        • The Natural Philosopher

          #5
          Re: implicit if() ?

          ursynek wrote:
          Lawrence Krubner pisze:
          >>
          >What is this syntax?
          >>
          >$hasMaxRecordL imit = ($this->getMaxRecordLi mit() !== false);
          >>
          >>
          >What is happening here? Is this some sort of implicit if statement? As
          >in "Assign the value of getMaxRecordLim it if it does not equal false"?
          >>
          >I noticed this line here:
          >>
          >http://trac.symfony-project.org/brow...s.php?rev=4659
          >>
          >
          >
          $a !== $b
          >
          Not identical TRUE if $a is not equal to $b, or they are not of
          the same type. (introduced in PHP 4)
          >
          I suppose that function $this->getMaxRecordLi mit() return true or false.
          So $hasMaxRecordLi mit will be 0 when $this->getMaxRecordLi mit() return
          false, otherwise will be 1.
          >
          =]
          >
          Sorry for my poor english :P
          Thats teh thing. IF() statements *branch* on true or false..but the
          expression inside the brackets can be used to set a variable directly,
          with no branch,

          I.e. (a==b) evaluates to TRUE or FALSE. As does (a>b), (a<b) (a!=b) etc
          etc..

          so c=(a-==b) will set c to a value of TRUE or FALSE.

          Comment

          • Sjoerd

            #6
            Re: implicit if() ?

            On Aug 7, 11:18 am, Erwin Moller
            $hasMaxRecordLi mit = ($this->getMaxRecordLi mit() !== false)
            I think this is equivalent:
            $hasMaxRecordLi mit = is_numeric($thi s->getMaxRecordLi mit());
            which is much clearer.
            If getMaxRecordLim it() only returns numbers or false, it would indeed
            give the same result. However, you probably think your method is
            clearer only because you are not used to the other method.

            Comment

            Working...