hi all,

Since the new conditionnal operator ternary was introduced in php 5.3, 
I'm little confuse about it.

The documentations says : 
Since PHP 5.3, it is possible to leave out the middle part of the ternary 
operator. Expression expr1 ?: expr3 returns expr1 if expr1  evaluates to 
TRUE, and expr3 otherwise.

I think it is not very usefull because most of the time, in PHP, we need 
to check the existance only of a var or return a default value.

$foo = isset($myArray['foo']) ? $myArray['foo'] : 'default';

I can't use the new syntax for that :

// raise a warning if $myArray['foo'] not exists and return 'default'
$foo = $myArray['foo'] ?: 'default'; 

// return 'default' if $myArray['foo'] not exists or equals '', 0, false, 
null 
$foo = @$myArray['foo'] ?: 'default';  

// return true or 'default' 
$foo = isset($myArray['foo']) ?: 'default';

This is the same thing like using if (isset($var)) instead of if ($var), 
developpers always use isset() because they known that cause a warning 
with array and this can be evaluated to false. 
If they want test if $var equals 0, '' or null, they use empty().

I don't know about you, but personnaly, I use certainly 99 % of the time 
isset() and 1% empty(). So if the short ternary operator would be more 
usefull if it just test the existance of a variable.

This is not a big problem but if a solution exists, this would be so 
cool ! Especialy when we have to check existance of twenty or more key in 
array. Code would be be lighter and clear.
Since i use PHP, I always have in my 'common function file' a function 
like that :

function getIssetVar($var, $default) { return ((isset($var)) ? $var : 
$default); }
 
So is it possible to make a little improvement on this operator or 
introduce a new operator or a core function which do that ? What's your 
feeling about it ?

-- 
Alban Leroux s...@paradoxal.org

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to