Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 10th, 2006, 04:05 AM
Russ.Dilley@gmail.com
Guest
 
Posts: n/a
Default 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.

  #2  
Old August 10th, 2006, 04:45 AM
yongjin.jiang@gmail.com
Guest
 
Posts: n/a
Default 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
Default 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
Default 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
Default 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles