My code:
function Check($Variable, $DefaultValue) {
if(isset($Variable) && $Variable != "" && $Variable != NULL) {
return $Variable;
}
else {
return $DefaultValue;
}
}
$a = Check(@$foo, false);
$b = Check(@$bar, "Hello");
//$a now equals false because $foo was not set.
//$b now equals "Hello" because $bar was not set.
isset()
check under the hood when testing a variable?EDIT:
The default value is there to be user defined. Sometimes it will be a number, sometimes a string.
We can pass the value 'undefined' to a function with multiple parameters using a variable that has the value of 'undefined'. In JavaScript, whenever a user declares a variable, it automatically contains the value of 'undefined'.
The unset() function is a predefined variable handling function of PHP, which is used to unset a specified variable. In other words, "the unset() function destroys the variables". The behavior of this function varies inside the user-defined function.
The unlink() function is used when you want to delete the files completely. The unset() Function is used when you want to make that file empty.
unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.
You can pass the variable to the method by reference by putting an & in front of it when declaring the function. Then you can just check if it set inside the function and not have to worry about suppressing warnings when passing the value that isn't set.
function Check(&$Variable, $DefaultValue) {
if(isset($Variable) && $Variable != "" && $Variable != NULL) {
return $Variable;
}
else {
return $DefaultValue;
}
}
$a = Check($foo, false);
$b = Check($bar, "Hello");
null
will be passedempty($var)
, e.g.: Check(empty($foo) ? null : $foo)
, although depending on the circumstances isset
may be more appropriateisset
does is exactly documented -- it tests if there is such a variable in scope and its value is not identical to null
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