Php shorthand isset and not empty

Stupid question, I know, but if I don't know whether a variable $var is set or not, should I use

isset($var) && !empty($var)

to check if it has any value in it, or is

!empty($var) enough? Would there be a problem if $var is null in the second case?

asked Nov 30, 2011 at 22:57

federico-tfederico-t

11.7k17 gold badges64 silver badges109 bronze badges

4

isset and empty are both language constructs. And empty() internally does an isset check first, then negates that, or alternatively also checks for values that equate FALSE in boolean context.

So yes, !empty() is sufficient.

answered Nov 30, 2011 at 23:00

7

Yes, you can drop the isset():

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

You can see this with the following code:

error_reporting(E_ALL);

var_dump(empty($var));
bool(true)

(Note the lack of an undefined variable warning)

answered Nov 30, 2011 at 23:00

Php shorthand isset and not empty

Tim CooperTim Cooper

153k37 gold badges319 silver badges272 bronze badges

You should use isset(), as !empty() will return false if your $var is 0.


answered Nov 30, 2011 at 23:01

Php shorthand isset and not empty

kylexkylex

13.9k31 gold badges112 silver badges173 bronze badges

1

I think you can drop that isset() completely in case you're using empty(), because empty() checks whether variable is set first, too

so after all, use

if (!empty($var)) {
    //isset and not empty
}

answered Nov 30, 2011 at 23:00

Martin.Martin.

10.3k3 gold badges40 silver badges67 bronze badges

0

PHP has two special shorthand conditional operators.

  • Ternary Operator
  • Null Coalescing Operator

PHP Ternary Operator

The ternary operator is a short way of performing an if conditional.

Ternary Syntax

() ? () : ()

  • If the condition is true, the result of on true expression is returned.
  • If the condition is false, the result of on false expression is returned.
  • Since PHP 5.3, you can omit the on true expression. (condition) ?: (on false) returns the result of condition if the condition is true. Otherwise, the result of on false expression is returned.

PHP Ternary Operator Example


'; // hello hyvor, because $username returns true

$username = null;
echo 'Hello ' . ($username ?: 'Guest') . '
'; // hello guest, because $username returns null // another example $var = 5; $isGreaterThan2 = $var > 2 ? true : false; var_dump($isGreaterThan2);

Run Example ››

It is not recommended to use nested expressions with the ternary operator. PHP's behavior when using more than one ternary operator within a single statement is non-obvious. So, use ternary operator only for simple conditions.

The ternary operator is commonly used with isset() function in PHP.

PHP Ternary Operator with Isset() Example


';

$fisrtname = 'Hyvor';
$lastname = 'Developer';
$username = (isset($fisrtname, $lastname)) ? $fisrtname . ' ' . $lastname : "Guest";
echo $username;

Run Example ››

Tip: The isset() function returns true if the variables (which are sent to the function in parameters) are set and not null.

Wrapping the expressions in the ternary operator with parentheses is optional.

PHP Null Coalescing Operator

PHP's null coalescing operator is a useful new feature which was introduced in PHP 7.

The null coalescing operator can be used to assign default values to a variable.

In the example below, the null coalescing expression returns 2 because $a is not defined.

PHP Null Coalescing Operator Example


Run Example ››

Important: Null coalescing operator allows for nesting.

PHP Null Coalescing Operator Nesting Example


Run Example ››

Does Isset check for empty?

The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.

What is isset ($_ GET?

Definition and Usage 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.

How use isset with ternary operator in PHP?

"default-value"; // which is synonymous to: $var = isset($array["key"]) ? $array["key"] : "default-value"; In PHP 5.3+, if all you are checking on is a "truthy" value, you can use the "Elvis operator" (note that this does not check isset).

Why Isset is used in PHP?

The isset function in PHP is used to determine whether a variable is set or not. A variable is considered as a set variable if it has a value other than NULL. In other words, you can also say that the isset function is used to determine whether you have used a variable in your code or not before.