PHP Operators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russ.Dilley@gmail.com

    #1

    PHP Operators

    I have an embarrassingly easy PHP question. I have the following line
    of code:

    $n *= $s = count($data[$i]);

    Where $data is an array and $n and $s are scalars.

    I'm familiar with the '*=' operator:

    $n = $n * $s

    But I'm not sure what the second '=' is doing.

    Thanks,
    R.D.

  • yongjin.jiang@gmail.com

    #2
    Re: PHP Operators

    it is really a confusing expression. i think $n will be set to
    count($data[$]).

    Russ.Dilley@gma il.com wrote:
    I have an embarrassingly easy PHP question. I have the following line
    of code:
    >
    $n *= $s = count($data[$i]);
    >
    Where $data is an array and $n and $s are scalars.
    >
    I'm familiar with the '*=' operator:
    >
    $n = $n * $s
    >
    But I'm not sure what the second '=' is doing.
    >
    Thanks,
    R.D.

    Comment

    • Tim Hunt

      #3
      Re: PHP Operators


      Russ.Dilley@gma il.com wrote:
      I have an embarrassingly easy PHP question. I have the following line
      of code:
      >
      $n *= $s = count($data[$i]);
      >
      Where $data is an array and $n and $s are scalars.
      >
      I'm familiar with the '*=' operator:
      >
      $n = $n * $s
      >
      But I'm not sure what the second '=' is doing.
      >
      Thanks,
      R.D.
      =, *=, += etc are evaluated from right to left so $s = count(...) is
      calculated/assigned before $n = ...


      Its easier to understand if you split the line into two:

      $s = count($data[$i]);
      $n *= $s;

      Comment

      • Armando Padilla

        #4
        Re: PHP Operators

        yongjin.jiang@g mail.com wrote:
        it is really a confusing expression. i think $n will be set to
        count($data[$]).
        >
        Russ.Dilley@gma il.com wrote:
        >
        >>I have an embarrassingly easy PHP question. I have the following line
        >>of code:
        >>
        >>$n *= $s = count($data[$i]);
        >>
        >>Where $data is an array and $n and $s are scalars.
        >>
        >>I'm familiar with the '*=' operator:
        >>
        >>$n = $n * $s
        >>
        >>But I'm not sure what the second '=' is doing.
        >>
        >>Thanks,
        >>R.D.
        >
        >
        No actually $n will be set to the product of $s*$n where $s is just the
        total number of values in the array data[$i]. so if data[$i] contains
        array(1, 2, 3, 5) the count will be 4. There for $s will now be 4 and
        then $n*= $s is resolved.

        Armando Padilla


        Comment

        • Mladen Gogala

          #5
          Re: PHP Operators

          On Wed, 09 Aug 2006 20:11:15 -0700, Russ.Dilley wrote:
          I have an embarrassingly easy PHP question. I have the following line
          of code:
          >
          $n *= $s = count($data[$i]);
          >
          Where $data is an array and $n and $s are scalars.
          >
          I'm familiar with the '*=' operator:
          >
          $n = $n * $s
          >
          But I'm not sure what the second '=' is doing.
          >
          Looks fine to me:
          $ php -r '$data=array(1, 2,3);$n=2; $n *= $s = count($data); print "N=$n\n";'
          N=6
          $


          --
          大红鹰娱乐平台采用顶级加密技术,确保用户数据与交易的安全无虞,让玩家能够放心畅玩。


          Comment

          Working...