Can anyone confirm if in PHP 0 = FALSE and anything else if TRUE?
Or is it more 0 = FALSE and 1 = TRUE?
One of the reasons I ask is that if I do;
//$count is an incrementing value starting from 0 in a loop
if($count % 2)
{
//do something
}
This works fine, but if I do:
if(!$count % 2)
{
//do something
}
It doesn't work. Should it not just be a boolean comparison? Why do I
need to do this to get my FALSE?
if($count % 2 === 0)
{
} 7 1637
The not operator (!) has higher precedence and is evaluated prior to
the mod operator.
You can use: if ($count % 2 == 0)
Precedence list: http://us.php.net/manual/en/language...ors.precedence
As for your other question about boolean comparisons, here are the
rules on boolean conversion: http://us2.php.net/manual/en/languag...oolean.casting
Regards,
John Peters
On May 30, 9:38 pm, Michael Sharman <sha...@gmail.comwrote:
Can anyone confirm if in PHP 0 = FALSE and anything else if TRUE?
Or is it more 0 = FALSE and 1 = TRUE?
One of the reasons I ask is that if I do;
//$count is an incrementing value starting from 0 in a loop
if($count % 2)
{
//do something
}
This works fine, but if I do:
if(!$count % 2)
{
//do something
}
It doesn't work. Should it not just be a boolean comparison? Why do I
need to do this to get my FALSE?
if($count % 2 === 0)
{
}
On May 31, 11:57 am, petersprc <peters...@gmail.comwrote:
The not operator (!) has higher precedence and is evaluated prior to
the mod operator.
You can use: if ($count % 2 == 0)
Precedence list:
http://us.php.net/manual/en/language...uage.operators...
As for your other question about boolean comparisons, here are the
rules on boolean conversion:
http://us2.php.net/manual/en/languag...#language.type...
Regards,
John Peters
On May 30, 9:38 pm, Michael Sharman <sha...@gmail.comwrote:
Can anyone confirm if in PHP 0 = FALSE and anything else if TRUE?
Or is it more 0 = FALSE and 1 = TRUE?
One of the reasons I ask is that if I do;
//$count is an incrementing value starting from 0 in a loop
if($count % 2)
{
//do something
}
This works fine, but if I do:
if(!$count % 2)
{
//do something
}
It doesn't work. Should it not just be a boolean comparison? Why do I
need to do this to get my FALSE?
if($count % 2 === 0)
{
}
Thanks for that John.
I suppose I can also do;
if(!($count % 2))
..oO(Michael Sharman)
>I suppose I can also do;
if(!($count % 2))
Yes. But even if this is documented behaviour, it's usually recommended
to explicitly write what you want to test for. In this case you're not
testing a boolean value ('!' is a logical operator), but an arithmetic
expression. So you should better write it as
if ($count % 2 == 0) {
...
}
This is cleaner and more readable code. Another example: The typical
shorthand way for testing if an array is empty or not is
if (!$someArray) {
...
}
But the better way is
if (empty($someArray)) {
...
}
Micha
On Sat, 31 May 2008 14:31:51 +0200, Michael Fesser <ne*****@gmx.dewrote:
.oO(Michael Sharman)
>I suppose I can also do;
if(!($count % 2))
Yes. But even if this is documented behaviour, it's usually recommended
to explicitly write what you want to test for. In this case you're not
testing a boolean value ('!' is a logical operator), but an arithmetic
expression. So you should better write it as
if ($count % 2 == 0) {
...
}
This is cleaner and more readable code. Another example: The typical
shorthand way for testing if an array is empty or not is
if (!$someArray) {
...
}
But the better way is
if (empty($someArray)) {
...
}
One could even argue using if(count($someArray) == 0) is better, as
empty() will not only work for arrays , but for a whole list of empty
values ('',0,null, etc.). Using the count function does immediatly issue
an error (non-fatal) when $someArray is not an array, making it more easy
to track bugs and errors. That being, said, I usually do use empty()
though :).
--
Rik Wasmus
....spamrun finished
..oO(Rik Wasmus)
>One could even argue using if(count($someArray) == 0) is better, as empty() will not only work for arrays , but for a whole list of empty values ('',0,null, etc.). Using the count function does immediatly issue an error (non-fatal) when $someArray is not an array, making it more easy to track bugs and errors. That being, said, I usually do use empty() though :).
ACK
The only annoying thing (IMHO) about empty() is that it only works on
variables, but sometimes I want to directly check a function result.
So I had to write a little helper function:
function isEmpty($var) {
return empty($var);
}
Now I can do things like
if (isEmpty(getSomething())) {
...
}
where the normal empty() would complain.
Micha
On May 31, 10:31 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Michael Sharman)
I suppose I can also do;
if(!($count % 2))
Yes. But even if this is documented behaviour, it's usually recommended
to explicitly write what you want to test for. In this case you're not
testing a boolean value ('!' is a logical operator), but an arithmetic
expression. So you should better write it as
if ($count % 2 == 0) {
...
}
This is cleaner and more readable code. Another example: The typical
shorthand way for testing if an array is empty or not is
if (!$someArray) {
...
}
But the better way is
if (empty($someArray)) {
...
}
Micha
Thanks Michael, that's a solid response
Michael Sharman wrote:
On May 31, 10:31 pm, Michael Fesser <neti...@gmx.dewrote:
>.oO(Michael Sharman)
>>I suppose I can also do; if(!($count % 2))
Yes. But even if this is documented behaviour, it's usually recommended to explicitly write what you want to test for. In this case you're not testing a boolean value ('!' is a logical operator), but an arithmetic expression. So you should better write it as
if ($count % 2 == 0) { ...
}
This is cleaner and more readable code. Another example: The typical shorthand way for testing if an array is empty or not is
if (!$someArray) { ...
}
But the better way is
if (empty($someArray)) { ...
}
Micha
Thanks Michael, that's a solid response
Some additional info on type comparison:
<URL:http://php.net/manual/en/types.comparisons.php>
--
Curtis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: greg |
last post by:
Discussion is invited on the following proto-PEP.
-------------------------------------------------------------
PEP ??? - Overloadable Boolean Operators...
|
by: Eyal |
last post by:
Hey,
I would appriciate if anyone can help on this one:
I have a java object/inteface having a method with a boolean
parameter. As I'm trying to call this method from a javascript it
fails on...
|
by: Henri |
last post by:
In java for instance there's a way to use booleans as objects and not as
value types.
I would like to do the same in VB.NET so that I can check if the boolean has
been explicitely defined (is not...
|
by: dba123 |
last post by:
Why am I getting this error for Budget?
Error: An exception of type 'System.FormatException' occurred in
mscorlib.dll but was not handled in user code
Additional information: String was not...
|
by: Shawnk |
last post by:
I would like to perform various boolean operations on bitmapped
(FlagsAttribute)
enum types for a state machine design as in;
-------------------
enum portState {
Unknown,
Open,
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |