eldan88 Posted March 28, 2012 Share Posted March 28, 2012 Hi, I am pretty sure this questions gets asked a lot. I would like to know what is the difference between those two. I know they share similarities. But from what I know a 0 integer or a 0 string is considered empty. How do I know when to use NULL and When to use empty ? Thanks! Link to comment https://forums.phpfreaks.com/topic/259850-booleans-what-is-the-difference-between-null-and-empty/ Share on other sites More sharing options...
rythemton Posted March 28, 2012 Share Posted March 28, 2012 It all depends on what you are doing: $a = ""; $b = 0; $c = NULL; $d = "0"; function $a $b $c $d is_numeric false true false true is_null false false true false is_string true false false true empty true true true true I've used it for multipurpose functions before: function multiPurpose( $value=NULL ) { if( is_null( $value ) ) { // this means no value was sent } elseif( is_numeric( $value ) ) { // this means a number was sent } elseif( is_array( $value ) ) { // this means an array was sent } else { // if it got here, it is none of the above, probably text } } And sometimes it doesn't really matter. Link to comment https://forums.phpfreaks.com/topic/259850-booleans-what-is-the-difference-between-null-and-empty/#findComment-1331775 Share on other sites More sharing options...
mr.noname Posted March 28, 2012 Share Posted March 28, 2012 in my opinion the NULL is not assign anything to the variable and most of default value are EMPTY value so if check it by empty() it return TRUE but if check by is_null() it should be return FALSE $a = NULL; // i think $a will not keep anything $b; // it assign to the default value --> i think it must be the empty value Link to comment https://forums.phpfreaks.com/topic/259850-booleans-what-is-the-difference-between-null-and-empty/#findComment-1331776 Share on other sites More sharing options...
rythemton Posted March 28, 2012 Share Posted March 28, 2012 $a = NULL; // i think $a will not keep anything $b; // it assign to the default value --> i think it must be the empty value Actually, both is_null( $a ) and is_null( $b ) will return TRUE responses. An unset or uninitialized variable is considered to have a NULL value. Setting a value to be NULL will also return a FALSE with the isset function, even though the value is set. Let's expand the chart: $a = ""; $b = 0; $c = NULL; $d = "0"; unset( $e ); function $a $b $c $d $e is_numeric false true false true false is_null false false true false true is_string true false false true false empty true true true true true isset true true false true false =="" true true true false true ==="" true false false false false Link to comment https://forums.phpfreaks.com/topic/259850-booleans-what-is-the-difference-between-null-and-empty/#findComment-1331780 Share on other sites More sharing options...
chrsm Posted March 28, 2012 Share Posted March 28, 2012 Hi, I am pretty sure this questions gets asked a lot. I would like to know what is the difference between those two. I know they share similarities. But from what I know a 0 integer or a 0 string is considered empty. How do I know when to use NULL and When to use empty ? Thanks! I would recommend reading the PHP manual, on the "type comparison" page: http://www.php.net/manual/en/types.comparisons.php in my opinion the NULL is not assign anything to the variable and most of default value are EMPTY value so if check it by empty() it return TRUE but if check by is_null() it should be return FALSE $a = NULL; // i think $a will not keep anything $b; // it assign to the default value --> i think it must be the empty value $a 'exists' but is set to NULL; isset and is_null will return FALSE and TRUE, respectively. NULL is not really a value, but the variable exists. For example: $ php -r 'if(is_null($b)) { echo("\$b is null\n"); }' PHP Notice: Undefined variable: b in Command line code on line 1 Notice: Undefined variable: b in Command line code on line 1 $b is null $b, in this case, was undefined, and something that isn't set is "NULL" in a way, but since it isn't set, it's going to trigger an error. If $b had be assigned NULL in a previous expression, there would be no warning triggered. Furthermore, there is no default value for a variable, so one cannot define a variable like so: $b; Without assigning it to anything. It will trigger an error if $b is referenced by an expression. $ php -r '$b; echo($b);' PHP Notice: Undefined variable: b in Command line code on line 1 Notice: Undefined variable: b in Command line code on line 1 Anyways, the tables on the page I linked have a very good explanation of how these things all map to each other. Cheers. Link to comment https://forums.phpfreaks.com/topic/259850-booleans-what-is-the-difference-between-null-and-empty/#findComment-1331781 Share on other sites More sharing options...
mr.noname Posted March 28, 2012 Share Posted March 28, 2012 Thank so much chrsm and rythemton is it not have the default value because php has a loosely typed language Link to comment https://forums.phpfreaks.com/topic/259850-booleans-what-is-the-difference-between-null-and-empty/#findComment-1331783 Share on other sites More sharing options...
rythemton Posted March 28, 2012 Share Posted March 28, 2012 I thought I'd check to make sure what I was saying was accurate and created a test script. Here are my results: Link to comment https://forums.phpfreaks.com/topic/259850-booleans-what-is-the-difference-between-null-and-empty/#findComment-1331785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.