Before doing something with $error:
$error = NULL;
In some script's saw:
$error = '';
$error = false;
$error = 0;
A variable is considered empty if it does not exist or if its value equals FALSE .
is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .
In PHP, a variable with no value is said to be of null data type. Such a variable has a value defined as NULL. A variable can be explicitly assigned NULL or its value been set to null by using unset() function.
Depends on your design:
NULL
.true
in case of error? Use false
.0
.''
.A better way to indicate errors would be throwing Exceptions though, rather than setting a variable and determine the error according to it.
There is no canonical answer to this question. As long as you use one of these semaphores consistently, you can use anything you want. Because PHP is loosely-typed, all of these values are "falsy" and can be evaluated in a boolean comparison as FALSE
.
That said, there is more of a difference between the empty string and the others, so I'd stick with NULL
s and FALSE
s in this sort of scenario.
1.
$v = NULL;
settype($v, 'string');
settype($v, 'int');
settype($v, 'float');
settype($v, 'bool');
settype($v, 'array');
var_dump($v);
2.
$v = NULL;
var_dump( (string) $v);
var_dump( (int) $v);
var_dump( (float) $v);
var_dump( (bool) $v);
var_dump( (array) $v);
It depends upon the conditions where you need to use the $error
. Using a NULL
is what I chose mostly as I deal more with MySQL clauses and all!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With