PHP is a weakly-typed language. By that I mean that variables are assigned values without regard to variable type, and implicit conversion at runtime sorts out any conflicts. PHP will happily let you compare a string and an integer, and if the string contains something unexpected, well, you should have paid more attention to data validation.
Given that the most important feature of PHP is the shallow learning curve, and that it is deployed in an environment where everything is a string, it’s (just) possible to argue that this was a good design decision. It allows you to do things like this:
$n = "5";
echo 3 + $n;
without getting into lots of tiresome explicit conversion. But the implicit rules are sometimes a little too clever for their own good, and throw up oddities like this:
$a = 'string';
$b = 0;
if ( $a == true && $b == false && $a == $b )
{
echo ( 'universe broken' );
}
What’s going on here? Lets look at these three clauses in detail:
( 'string' == true )because any non-null string evaluates totruewhen compared with a boolean( 0 == false )because the integer0undergoes implicit conversion to boolean and evaluates tofalse- Finally,
( 'string' == 0 )because a string is silently promoted to integer when compared with an integer. If the string is the ASCII representation of a number (eg"123"), it is assigned that value. If it doesn’t contain a number, it is assigned the value 0. So our third clause evaluates astrue. Oops…
I routinely use the triple-equals operator (identical) instead of the double-equals operator (equality) now, but it feels and looks like a hack to avoid someone else’s bad design decision biting me. PHP is chock-full of these little oddities.
If you want all the gory details, the type comparison tables are tucked deep inside the PHP Manual.
Update: This post over at ycombinator contains an excellent one-line summary of what I was getting at.
Tags: php
10 comments
Comments feed for this article
Trackback link
http://www.otton.org/2008/08/06/stupid-php-tricks-true-false-comparison/trackback/
August 7, 2008 at 10:09 pm
Pingback from AOWS » true == false
August 8, 2008 at 5:28 am
Pingback from php to python. Why? | jodhpuriGuy | Avi Mehta
August 9, 2008 at 9:43 am
Pingback from PHP: true == false | Bao’s blog
August 10, 2008 at 5:01 pm
Pingback from Link Post Sunday 08/10 | Mr Sun Studios
August 9, 2008 at 11:15 pm
johnjo
This is a good post, but here’s my opinion. It’s not an oddity.
Lower level languages have to enable strict typing because you’re dealing directly with an assembler.
PHP is a high level SCRIPTING language (which is very powerful and fast BTW) that doesn’t need to enable strict typing because it can check as it runs. Its just that as a scripting language where you’re not compiling things BEFORE they run, but rather AS they are running, you don’t want to save your file with a mistake in it as simple as a missed type declaration, or a comparison between two unfamiliar types and then have your entire application break… that would be poor design on the PHP team’s part.
In your example I don’t really think that the 3rd clause is an “Oops”. Why would you want to compare a string to zero, ever? I think that questioning this is bogus in the first place. Although this would be a simple (yet poor) check to validate data in some cases. I don’t see this as a mistake on PHP’s end. Error prevention is important in any language. Yes it enables people to generate sloppy code, but I have seen even sloppier code written in C, Java, and even ASM.
In general this is really up to the programmer to be good at what they do, PHP is just there to allow you to make minor mistakes in a production environment. That’s all you’re talking about, something very minor.
August 13, 2008 at 5:47 pm
Omar
I think that oddity mentioned before is an small concession despite of the fast development you won. That’s the power of scripting languages.
BTW I’m a Java Developer, when I need speed I go for groovy.
August 16, 2008 at 9:50 am
alex
Small Remark: “any non-null string evaluates to true when compared with a boolean” is not exactly true, the string ‘0′ also evaluates to false. I think this is a hardcoded exception not due to integer conversion, because the string ‘0.0′ evaluates to true.
August 16, 2008 at 12:13 pm
CPP
Following “johnjo” comment: Don’t you people think it’s a bit sad that people in the industry start looking at PHP and ASP and even actionscript as a programming language rather than a script?!
I heard a while ago from someone that in his company they look at C++ as a low level programming language!
I think that in 10 years from now there will be such a few people that will know C/C++/ASM that we will have super computers, but they will run like 80286 because the OS will be written in ActionScript It starts happening today - not kidding! we have super computers that run a bit slow because too much code that should be written efficiently is written in .NET or Java, meaning using interpreters, meaning losing of efficency!
Anyway, I’m really waiting to the day that drivers programmer will get huge salary because there will be 10 of those in the world! ;-)
August 25, 2008 at 6:45 am
David Ackerman
CPP, you are being sensationalist. The faster computers get, the more can be done in the background to help development. If you had to code in Assembly language today, we would have really fast, really memory efficient, BROKEN programs. Programming is a battle between the human mind, and the program’s complexity. With languages like .NET, Java, PHP, Python, etc., you are reducing the human brain’s requirement for complexity and trading off a (relatively slight) performance hit. I will take that tradeoff any day.
August 25, 2008 at 3:10 pm
Oliver Ong
CPP, um i would rather use php to do websites than use c/c++ or even asm. lets see who finishes creating a db add edit and delete transaction first.