Page 1 of 1

[SOLVED] && || valid in PHP?

Posted: Thu Feb 05, 2009 8:43 pm
by arya6000
Hi

I'm a C programmer and I'm learning php now, I wrote a program something like

Code: Select all

 
while($row = mysql_fetch_array($result) && $i < 250)
{
....
.....
}
 
But it does not work. I tried to find some reference online for using && and || in php loops and I did not find anything. Does php support && and || in loops?

Thank You

Re: && || valid in PHP?

Posted: Thu Feb 05, 2009 8:55 pm
by John Cartwright
Indeed PHP does support both. More info can be found on the operators page, and more specifically the logical operators page.

PHP also supports AND and OR, although they hold different precedence.

Can you define "does not work"?

Re: && || valid in PHP?

Posted: Thu Feb 05, 2009 9:06 pm
by requinix
John Cartwright wrote:PHP also supports AND and OR, although they hold different precedence.
Yay! Only thing missing is a link to the operator precedence page.

As a C programmer you should be familiar with the concept of precedence. Notice how && is higher in the table than =?

Re: && || valid in PHP?

Posted: Thu Feb 05, 2009 9:12 pm
by arya6000
ok, thanks it working now.

All I needed was to put them in brackets like

while(($row = mysql_fetch_array($result)) && ($i < 250))

Re: && || valid in PHP?

Posted: Thu Feb 05, 2009 10:52 pm
by Skoalbasher
arya6000 wrote:ok, thanks it working now.

All I needed was to put them in brackets like

while(($row = mysql_fetch_array($result)) && ($i < 250))
Doh! lol