Skip to content

Instantly share code, notes, and snippets.

@danrichards
Last active June 5, 2020 02:53
Show Gist options
  • Save danrichards/16fd15e14d6d841eec80 to your computer and use it in GitHub Desktop.
Save danrichards/16fd15e14d6d841eec80 to your computer and use it in GitHub Desktop.
PHP for those who love ternaries

PHP Ternary cheat sheet

Be careful with your ternaries, folks.

var_dump('' ?: 'empty stringy');                 // empty string
var_dump(null ?: 'nully');                       // nully
var_dump(0 ?: 'zeroy');                          // zeroy
var_dump(false ?: 'falsey');                     // falsey
var_dump(new stdClass() ?: 'objecty');           // stdClass Object ( )
var_dump([] ?: 'array-y');                       // array-y

$var = [];
var_dump($var['out_of_bounds'] ?: 'out_of_boundsey');   // fatal
@danrichards
Copy link
Author

Test comment from Dan with annotation. @uptag

@uptag
Copy link

uptag commented Apr 3, 2016

Response from the other guy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment