PHP abbreviation for isset ()?

Is there a shorthand way to assign a variable to something if it does not exist in PHP?

if(!isset($var) { $var = ""; } 

I would like to do something like

 $var = $var | ""; 
+31
php isset
Sep 03 '13 at 23:52
source share
2 answers

Update for PHP 7 (thanks to shock_gone_wild )

PHP 7 introduces the so-called zero-coalescence operator, which simplifies the following statements:

 $var = $var ?? "default"; 

Before PHP 7

No, there is no special operator or special syntax for this. However, you can use the ternary operator:

 $var = isset($var) ? $var : "default"; 

Or like this:

 isset($var) ?: $var = 'default'; 
+99
Sep 03 '13 at 23:55 on
source share

You can use the new ternary operator (PHP 5.3 +)

 isset($var) ?: $var = ""; 

Or for an older version:

 $var = isset($var) ? $var : ""; 
+16
03 Sep '13 at 23:57
source share



All Articles