Is there a function to check both
if (isset($var) && $var)
?
PHP isset() Function The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
The method is used to GET the information or POST the information as entered in the form. Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method. Remember in place of submit define the name of submit button.
is_bool($var): The function is_bool() checks whether a variable's type is Boolean and returns true or false. Comparison using “===” or ($var===true || $var===false): If you compare the variable with the operator “===” with true and false and if it is identical to one of the values, then it is a boolean value.
isset( $_POST['submit'] ) : This line checks if the form is submitted using the isset() function, but works only if the form input type submit has a name attribute (name=”submit”).
The empty()
function will do the job.
Use it with the not operator (!
) to test "if not empty", i.e.
if(!empty($var)){ }
You may use the ?? operator as such:
if($var ?? false){ ... }
What this does is checks if $var
is set and keep it's value. If not, the expression evaluates as the second parameter, in this case false
but could be use in other ways like:
// $a is not set $b = 16; echo $a ?? 2; // outputs 2 echo $a ?? $b ?? 7; // outputs 16
More info here: https://lornajane.net/posts/2015/new-in-php-7-null-coalesce-operator
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