PHP | Different ways of comparing variables

on Wednesday 11 December 2013
A comment on PHP got me thinking on the ways variables represent themselves. PHP has a funky way of variable handling. I say this because enforcing a variable type is something unheard of in PHP. Variables can be juggled by type. This is an inconsistency, and yes, certainly you can make checks for this. Yet this is not the point of my post.

I will start off by stating that the post on php.net inspired me to make this. I wanted to know the extend of the case he was trying to make and see if my results came up with anything else. This comment, at point of writing, is already 5 years old. 5 years is a long time, a lot of things have changed. Zend has taken php under their wings of development.

I will also say that these test results are my findings. Meaning that what I parse off as false, may indeed be true. I might lack the knowledge or logics to see weither the mechanics are broken or not. I'm a home-schooled programmer who just tries to find a better understanding.

In this post I will parse off different variable types and case some common comparisons. These tests gave back some suprising results. I did not expect some of these results. Now, bear in mind these tests have been done with == and do not check the variable type. Yet still. Lets get started with my main motivation to write this post. The variable type NULL.

Function used
NULL
null
"null"
'null'
0
"0"
'0'
is_null($var)
!is_null($var)
$var == NULL
$var != NULL
$var == null
$var == 0
$var == '0'
$var == "0"
$var == ""
!$var
$var == true
$var == false
isset($var)
!isset($var)
empty($var)
!empty($var)

Above the "suprising" results are marked. I can see the pink ones making some sence, but the coral coloured onces not making sense at all. I expected the opposite to happen. You might not run into these situations often, but when you do, things get tough to debug. Especially when it is nested into some pretty complex codes. I happen to run into these situations when updating ancient ~ not my own ~ coding.

Booleans are the second type I will take under a test. I did this because I thought to have had an similar situation with them, but as the test results show, they react as expected. So, I have nothing really to show with these results, yet I will put them out there as testing these took me some time.

Function used
true
"true"
1
false
"false"
0
NULL
$var == 1
$var == true
$var != true
is_bool($var)
!is_bool($var)
isset($var)
!isset($var)
empty($var)
!empty($var)
is_numeric($var)

So that really concludes my last hour of testing variables in PHP. Now, this might seem a little bit pointless, yet now can reference to this when I'm in doubt. It might help somebody else aswell.

I did some more testing on strings and integers aswell, yet those results were similar to those of booleans and did not find them noteworthy enough to include in the post.

Feel free to comment, I will update this article when additional information gets to me. Either do it through G+ or in the comment section below.

If this was a great read and you happen to be on G+, then hit the +1 button!

~ Rob

0 comments:

Post a Comment