Anti Veeranna

The pain, the pleasure ..

More PHP type juggling, #2

leave a comment »

I found the following gem over at Stackoverflow.com today. Given the following code, what do you think the result is?

<?php
var_dump("01a4" == "001a4");
var_dump("01e4" == "001e4");

The correct answer is:
bool(false)
bool(true)

Why?

PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates “01e4″ == “10000″ as true because these are numbers with equivalent values.

Previously.

Advertisement

Written by Anti Veeranna

2010/09/12 at 10:00

Posted in Pain

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.