Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Operators

Russ.Dilley@gmail.com
Guest
 
Posts: n/a
#1: Aug 10 '06
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
Guest
 
Posts: n/a
#2: Aug 10 '06

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.
Tim Hunt
Guest
 
Posts: n/a
#3: Aug 10 '06

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;

Armando Padilla
Guest
 
Posts: n/a
#4: Aug 10 '06

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


Mladen Gogala
Guest
 
Posts: n/a
#5: Aug 10 '06

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