Test Various “Nothings” in PHP

Following php codes is to test six different “nothing”variables together with popular functions, plus number 1 as control.

‹?php
$undefinedVar;
$nullVar = null;
$strEmpty = "";
$strChanged = "initial value";
$numZero = 0;
$numChanged = 1;
$numOne = 1;

$strChanged = null;
$numChanged = null;

echo "‹br/›11. undefinedVar: (".$undefinedVar.")";
echo "‹br/›12. nullVar: (".$nullVar.")";
echo "‹br/›13. strEmpty: (".$strEmpty.")";
echo "‹br/›14. strChanged: (".$strChanged.")";
echo "‹br/›15. numZero: (".$numZero.")";
echo "‹br/›16. numChanged: (".$numChanged.")";

echo "‹br/›21. is_null-undefinedVar: (".is_null($undefinedVar).")";
echo "‹br/›22. is_null-nullVar: (".is_null($nullVar).")";
echo "‹br/›23. is_null-strEmpty: (".is_null($strEmpty).")";
echo "‹br/›24. is_null-strChanged: (".is_null($strChanged).")";
echo "‹br/›25. is_null-numZero: (".is_null($numZero).")";
echo "‹br/›26. is_null-numChanged: (".is_null($numChanged).")";

echo "‹br/›31. isset-undefinedVar: (".isset($undefinedVar).")";
echo "‹br/›32. isset-nullVar: (".isset($nullVar).")";
echo "‹br/›33. isset-strEmpty: (".isset($strEmpty).")";
echo "‹br/›34. isset-strChanged: (".isset($strChanged).")";
echo "‹br/›35. isset-numZero: (".isset($numZero).")";
echo "‹br/›36. isset-numChanged: (".isset($numChanged).")";

echo "‹br/›47. TRUE: (".TRUE.")";
echo "‹br/›48. FALSE: (".FALSE.")";

echo "‹br/›51. empty-undefinedVar: (".empty($undefinedVar).")";
echo "‹br/›52. empty-nullVar: (".empty($nullVar).")";
echo "‹br/›53. empty-strEmpty: (".empty($strEmpty).")";
echo "‹br/›54. empty-strChanged: (".empty($strChanged).")";
echo "‹br/›55. empty-numZero: (".empty($numZero).")";
echo "‹br/›56. empty-numChanged: (".empty($numChanged).")";
echo "‹br/›57. empty-numOne: (".empty($numOne).")";

echo "‹br/›61. defined-undefinedVar: (".defined($undefinedVar).")";
echo "‹br/›62. defined-nullVar: (".defined($nullVar).")";
echo "‹br/›63. defined-strEmpty: (".defined($strEmpty).")";
echo "‹br/›64. defined-strChanged: (".defined($strChanged).")";
echo "‹br/›65. defined-numZero: (".defined($numZero).")";
echo "‹br/›66. defined-numChanged: (".defined($numChanged).")";

echo "‹br/›71. strlen-trim-undefinedVar: (".strlen(trim($undefinedVar)).")";
echo "‹br/›72. strlen-trim-nullVar: (".strlen(trim($nullVar)).")";
echo "‹br/›73. strlen-trim-strEmpty: (".strlen(trim($strEmpty)).")";
echo "‹br/›74. strlen-trim-strChanged: (".strlen(trim($strChanged)).")";
echo "‹br/›75. strlen-trim-numZero: (".strlen(trim($numZero)).")";
echo "‹br/›76. strlen-trim-numChanged: (".strlen(trim($numChanged)).")";
?›

unset() does not work for any one of them. Therefore, it had been excluded.

Test results in both Firefox and IE are identical, as follows:

11. undefinedVar: ()
12. nullVar: ()
13. strEmpty: ()
14. strChanged: ()
15. numZero: (0)
16. numChanged: ()
21. is_null-undefinedVar: (1)
22. is_null-nullVar: (1)
23. is_null-strEmpty: ()
24. is_null-strChanged: (1)
25. is_null-numZero: ()
26. is_null-numChanged: (1)
31. isset-undefinedVar: ()
32. isset-nullVar: ()
33. isset-strEmpty: (1)
34. isset-strChanged: ()
35. isset-numZero: (1)
36. isset-numChanged: ()
47. TRUE: (1)
48. FALSE: ()
51. empty-undefinedVar: (1)
52. empty-nullVar: (1)
53. empty-strEmpty: (1)
54. empty-strChanged: (1)
55. empty-numZero: (1)
56. empty-numChanged: (1)
57. empty-numOne: ()
61. defined-undefinedVar: ()
62. defined-nullVar: ()
63. defined-strEmpty: ()
64. defined-strChanged: ()
65. defined-numZero: ()
66. defined-numChanged: ()
71. strlen-trim-undefinedVar: (0)
72. strlen-trim-nullVar: (0)
73. strlen-trim-strEmpty: (0)
74. strlen-trim-strChanged: (0)
75. strlen-trim-numZero: (1)
76. strlen-trim-numChanged: (0)

Observed, included but not limited to this test:

1. Results in Firefox and IE are exactly same. However, if an null variable passed from JavaScript to PHP, in IE, it would be shown as a string "null". In a real case scenario, a variable is to pass from form feeding in HTML/JavaScript to PHP. However, it had only be assigned with value “null”. It supposes an integer data type. Unfortunately, in is_null() in PHP, it results not null, while echo shows “null” in IE and nothing in Firefox.
2. unset() does not work at all for all of them.
3. is_null() works fine. It however regards empty string as not null, which should be.
4. isset() works fine. It regards empty string as set.
5. is_null() is just opposite to isset().
6. empty() works for all, including regarding number 0 as empty.
7. defined() is to detect if a constant string exists, which does works fine here, since no constant string here.
8. strlen(trim()) does work fine except number 0, which should be.

Conclusion:

First of all, never use null in JavaScript where it supposed to be a string. Use '' instead. This is because in IE, when JavaScript passes string to PHP, null would become 'null'.

1. If one want to include everything, null, empty string, number 0, and even string “0”, the best approach is to use if(empty($var)).

2. If one wants to include null and empty string and exclude number 0, if(0==strlen(trim($var)) might be best approach. However, this approach is unable to detect the string "null".

3. If one wants to include null and number 0 and exclude empty string, the best approach maybe if(is_null($var) || 0==$var).

4. If one wants to only include null and exclude empty string and number 0, the best approach is if(is_null($var)). However, since both JavaScript and PHP does not tell the data type when declare a variable, it somehow easily to be mixed up the undefined variable or null variable with empty string. It is therefore suggested extra caution shall be applied to exclude empty string.

5. In most case people dealing with null would include undefined variable, null variable, empty string in JavaScript and PHP. For special case mentioned above for IE, it would includes string "null" as well, which isn't covered by Conclustion 2. So, the possible most save approach is put two together:

if(0==strlen(trim($var)) || "null"==$var)

6. To deal with undefined array, please refer to http://koncordpartners.blogspot.com/2009/12/number-of-elements-of-array.html.

http://ca2.php.net/manual/en/function.empty.php

No comments:

Post a Comment

Labels