Since a year or so I had a function in all my projects called: ifNot()
.
This function is somehow a derivated from the ternary operator:
public function ifNot( $item, $ifNot )
{
if ( !$item ) {
return $ifNot
} else {
return $item;
}
}
// The var $foo twice
$var = ( $foo ) ? $foo : 'is empty or false =(';
// The var $foo once
$var = ifNot($foo, 'is empty or false =(');
In fact, Twig Template Engine use something like it:
{{ foo ? 'yes' : 'no' }}
{# as of Twig 1.12.0 #}
{{ foo ?: 'no' }} == {{ foo ? foo : 'no' }}
{{ foo ? 'yes' }} == {{ foo ? 'yes' : '' }}
The question is: There is a better/known approach for this function?
PS: SQL also has an IFNULL()
=)