Connecting Tech Pros Worldwide Help | Site Map

PHP Operators

  #1  
Old August 10th, 2006, 04:05 AM
Russ.Dilley@gmail.com
Guest
 
Posts: n/a
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.

  #2  
Old August 10th, 2006, 04:45 AM
yongjin.jiang@gmail.com
Guest
 
Posts: n/a

re: PHP Operators


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

Russ.Dilley@gmail.com wrote:
Quote:
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.
  #3  
Old August 10th, 2006, 05:35 AM
Tim Hunt
Guest
 
Posts: n/a

re: PHP Operators



Russ.Dilley@gmail.com wrote:
Quote:
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 = ...
http://www.php.net/manual/en/languag...ors.precedence

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

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

  #4  
Old August 10th, 2006, 07:15 AM
Armando Padilla
Guest
 
Posts: n/a

re: PHP Operators


yongjin.jiang@gmail.com wrote:
Quote:
it is really a confusing expression. i think $n will be set to
count($data[$]).
>
Russ.Dilley@gmail.com wrote:
>
Quote:
>>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


  #5  
Old August 10th, 2006, 03:45 PM
Mladen Gogala
Guest
 
Posts: n/a

re: PHP Operators


On Wed, 09 Aug 2006 20:11:15 -0700, Russ.Dilley wrote:
Quote:
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
$


--
http://www.mgogala.com

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Attempting to use Bitwise Operators Michael B. Trausch answers 9 July 17th, 2005 02:49 PM
PHP, MySQL & Limiting Access Jay Moore answers 2 July 17th, 2005 01:12 AM