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 seza@paradoxal.org
On Friday 20 November 2009 11:12:29 pm Alban wrote:
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.
I cannot comment on the rest of your post right now, but if you are checking
for the existence of a bunch of keys in an associative array and setting
defaults if they are not set, the following will be considerably faster and
easier to read:
$my_array += array(
'a' => 'A',
'b' => 'B',
'c' => 'C',
);
That will set $my_array['a'] to A iff it doesn't exist, ['b'] to B iff it
doesn't exist, etc. That is far nicer to read than a bunch of ternaries,
short-circuited or no. You can even stick the defaults array into a function
and call it from various places to ensure your array always has the same sane
defaults.
--
Larry Garfield
larry@garfieldtech.com
Le Fri, 20 Nov 2009 23:28:39 -0600, Larry Garfield a écrit :
On Friday 20 November 2009 11:12:29 pm Alban wrote:
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.I cannot comment on the rest of your post right now, but if you are
checking for the existence of a bunch of keys in an associative array
and setting defaults if they are not set, the following will be
considerably faster and easier to read:$my_array += array(
'a' => 'A',
'b' => 'B',
'c' => 'C',
);That will set $my_array['a'] to A iff it doesn't exist, ['b'] to B iff
it doesn't exist, etc. That is far nicer to read than a bunch of
ternaries, short-circuited or no. You can even stick the defaults array
into a function and call it from various places to ensure your array
always has the same sane defaults.
Yes, union operator is a pretty solution for arrays.
--
Alban Leroux seza@paradoxal.org
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 ?
this feature request has already been discussed and declined:
http://wiki.php.net/rfc/ifsetor
please review this rfc before continuing this thread.
regards,
Lukas Kahwe Smith
mls@pooteeweet.org
Le Sat, 21 Nov 2009 09:48:10 +0100, Lukas Kahwe Smith a écrit :
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 ?this feature request has already been discussed and declined:
http://wiki.php.net/rfc/ifsetorplease review this rfc before continuing this thread.
regards,
Lukas Kahwe Smith
mls@pooteeweet.org
Thanks for the link to the RFC :)
Excuse me, but I'll be little hard in this post. This for insult the
community but I want the community really think about the decision it
made and the reason why.
I also read why it have been refused here :
http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-
foo-isset-foo-foo-something-else
Is it serious ?
«
The name for this new operator is heavily disputed and we could not agree
on a decent name for it.
»
Tomorrow I will not send food to the association for children who are
hungry because I can not choose between offering Thai or basmati rice.
Stop sarcasm, seriously, this is not an honorable response from people
making decisions. Take your responsibility and make a vote or impose a
name, just do it.
«
Instead of implementing ifsetor() we remove the
requirement for the "middle" parameter to the ?: operator.
»
That's not people wants and that's not do their need.
So that not a correct answer of the php developper demand.
«
In combination with the new input_filter extension
you then reach the original goal of setting a default
value to a non-set input variable with:
$blahblah = input_filter_get(GET, 'foo', FL_INT) ?: 42;
»
I don't see how do that with the actual filter extension. Even if it is
possible, this is not a pretty short and easier solution than :
$var = (isset($var)) ? $var : 'default';
Why not add a simple new operator who do the job, this is not needing a
name :
// set a default value if $var is not set
$var ?= 'default';
// equalivalent to :
$var = (isset($var)) ? $var : 'default';
--
Alban Leroux seza@paradoxal.org
Alban wrote:
Le Sat, 21 Nov 2009 09:48:10 +0100, Lukas Kahwe Smith a écrit :
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 ?this feature request has already been discussed and declined:
http://wiki.php.net/rfc/ifsetorplease review this rfc before continuing this thread.
regards,
Lukas Kahwe Smith
mls@pooteeweet.orgThanks for the link to the RFC :)
Excuse me, but I'll be little hard in this post. This for insult the
community but I want the community really think about the decision it
made and the reason why.I also read why it have been refused here :
http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-
foo-isset-foo-foo-something-elseIs it serious ?
«
The name for this new operator is heavily disputed and we could not agree
on a decent name for it.
»Tomorrow I will not send food to the association for children who are
hungry because I can not choose between offering Thai or basmati rice.Stop sarcasm, seriously, this is not an honorable response from people
making decisions. Take your responsibility and make a vote or impose a
name, just do it.«
Instead of implementing ifsetor() we remove the
requirement for the "middle" parameter to the ?: operator.
»That's not people wants and that's not do their need.
So that not a correct answer of the php developper demand.«
In combination with the new input_filter extension
you then reach the original goal of setting a default
value to a non-set input variable with:$blahblah = input_filter_get(GET, 'foo', FL_INT) ?: 42;
»I don't see how do that with the actual filter extension. Even if it is
possible, this is not a pretty short and easier solution than :$var = (isset($var)) ? $var : 'default';
The ternary isn't meant to solve the isset thing you are talking about.
It is simply a shortcut to normal ternary operations. The most common
case where you don't know if a variable is set is on the initial input
via $_GET or $_POST and we definitely don't want people doing:
$var = $_GET['foo'] ?: 42;
It would be an XSS disaster. Hence the suggestion to use input_filter
there, or a similar user-supplied filtering function in which case the
ternary, as it is currently implemented, is perfectly suitable.
-Rasmus
More interesting behaviors to dig are there:
variable = <value1> ?? <value2>;
variable = <value0> ? <value4> : <value1> ?? <value2>;
or a la javascript
variable = <value1> || <value2>;
Best,
Alban wrote:
Le Sat, 21 Nov 2009 09:48:10 +0100, Lukas Kahwe Smith a écrit :
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 ?this feature request has already been discussed and declined:
http://wiki.php.net/rfc/ifsetorplease review this rfc before continuing this thread.
regards,
Lukas Kahwe Smith
mls@pooteeweet.orgThanks for the link to the RFC :)
Excuse me, but I'll be little hard in this post. This for insult the
community but I want the community really think about the decision it
made and the reason why.I also read why it have been refused here :
http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-
foo-isset-foo-foo-something-elseIs it serious ?
«
The name for this new operator is heavily disputed and we could not agree
on a decent name for it.
»Tomorrow I will not send food to the association for children who are
hungry because I can not choose between offering Thai or basmati rice.Stop sarcasm, seriously, this is not an honorable response from people
making decisions. Take your responsibility and make a vote or impose a
name, just do it.«
Instead of implementing ifsetor() we remove the
requirement for the "middle" parameter to the ?: operator.
»That's not people wants and that's not do their need.
So that not a correct answer of the php developper demand.«
In combination with the new input_filter extension
you then reach the original goal of setting a default
value to a non-set input variable with:$blahblah = input_filter_get(GET, 'foo', FL_INT) ?: 42;
»I don't see how do that with the actual filter extension. Even if it is
possible, this is not a pretty short and easier solution than :$var = (isset($var)) ? $var : 'default';
The ternary isn't meant to solve the isset thing you are talking about.
It is simply a shortcut to normal ternary operations. The most common
case where you don't know if a variable is set is on the initial input
via $_GET or $_POST and we definitely don't want people doing:$var = $_GET['foo'] ?: 42;
It would be an XSS disaster. Hence the suggestion to use input_filter
there, or a similar user-supplied filtering function in which case the
ternary, as it is currently implemented, is perfectly suitable.-Rasmus
I would love to restate my recommendation for the function "filled".
Which is the opposite of "empty". Filled would accept a variable
number of arguments and return the first where empty evaluates as
false.
Like empty, filled would not throw notices for undefined variables.
This is not the same as the ifsetor debate because filled is opposite
empty and cares not about isset.
-- dante
Alban wrote:
Le Sat, 21 Nov 2009 09:48:10 +0100, Lukas Kahwe Smith a écrit :
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 ?this feature request has already been discussed and declined:
http://wiki.php.net/rfc/ifsetorplease review this rfc before continuing this thread.
regards,
Lukas Kahwe Smith
mls@pooteeweet.orgThanks for the link to the RFC :)
Excuse me, but I'll be little hard in this post. This for insult the
community but I want the community really think about the decision it
made and the reason why.I also read why it have been refused here :
http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-
foo-isset-foo-foo-something-elseIs it serious ?
«
The name for this new operator is heavily disputed and we could not agree
on a decent name for it.
»Tomorrow I will not send food to the association for children who are
hungry because I can not choose between offering Thai or basmati rice.Stop sarcasm, seriously, this is not an honorable response from people
making decisions. Take your responsibility and make a vote or impose a
name, just do it.«
Instead of implementing ifsetor() we remove the
requirement for the "middle" parameter to the ?: operator.
»That's not people wants and that's not do their need.
So that not a correct answer of the php developper demand.«
In combination with the new input_filter extension
you then reach the original goal of setting a default
value to a non-set input variable with:$blahblah = input_filter_get(GET, 'foo', FL_INT) ?: 42;
»I don't see how do that with the actual filter extension. Even if it is
possible, this is not a pretty short and easier solution than :$var = (isset($var)) ? $var : 'default';
The ternary isn't meant to solve the isset thing you are talking about.
It is simply a shortcut to normal ternary operations. The most common
case where you don't know if a variable is set is on the initial input
via $_GET or $_POST and we definitely don't want people doing:$var = $_GET['foo'] ?: 42;
It would be an XSS disaster. Hence the suggestion to use input_filter
there, or a similar user-supplied filtering function in which case the
ternary, as it is currently implemented, is perfectly suitable.-Rasmus
--
--
Sent from my mobile device
D. Dante Lorenso
dante@lorenso.com
972-333-4139
I would love to restate my recommendation for the function "filled".
Which is the opposite of "empty". Filled would accept a variable
number of arguments and return the first where empty evaluates as
false.Like empty, filled would not throw notices for undefined variables.
This is not the same as the ifsetor debate because filled is opposite
empty and cares not about isset.
did you even read the RFC?
regards,
Lukas Kahwe Smith
mls@pooteeweet.org
Lukas Kahwe Smith wrote:
I would love to restate my recommendation for the function "filled".
Which is the opposite of "empty". Filled would accept a variable
number of arguments and return the first where empty evaluates as
false.Like empty, filled would not throw notices for undefined variables.
This is not the same as the ifsetor debate because filled is opposite
empty and cares not about isset.did you even read the RFC?
Yes I did, and all I see is this in the References section:
"Suggestion to leave an empty() variant out of the picture since
this feature can be implemented in userland, though this of
course not provide the full functionality of empty() which
does not trigger notices for missing variables"
I didn't see my proposal listed in it anywhere. See this recommendation
from 3 1/2 years ago:
- May 03, 2006
internals@lists.php.net/msg21617.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg21617.html
Can someone please add the 'filled' proposal to the RFC? I find
'filled' way more useful than 'ifsetor' because in everyday code, I am
constantly wanting to assign default values to variables that don't have
values for a variety of reasons. The assignment of a default value
happens before input filtering.
$email = filled(
$_REQUEST['email'],
$CONFIG->email_default,
$class_email,
'default@domain'
);
Give me the first non-empty value and don't throw NOTICE warnings about
it. Otherwise, I need all this:
$email = !empty($_REQUEST['email']) ? $_REQUEST['email'] : (
!empty($CONFIG->email_default) ? $CONFIG->email_default : (
!empty($class_email) ? $class_email : 'default@domain'
));
Yuck.
-- Dante
D. Dante Lorenso
dante@lorenso.com
972-333-4139
Lukas Kahwe Smith wrote:
I would love to restate my recommendation for the function "filled".
Which is the opposite of "empty". Filled would accept a variable
number of arguments and return the first where empty evaluates as
false.Like empty, filled would not throw notices for undefined variables.
This is not the same as the ifsetor debate because filled is opposite
empty and cares not about isset.
did you even read the RFC?Yes I did, and all I see is this in the References section:
"Suggestion to leave an empty() variant out of the picture since
this feature can be implemented in userland, though this of
course not provide the full functionality of empty() which
does not trigger notices for missing variables"I didn't see my proposal listed in it anywhere. See this recommendation from 3 1/2 years ago:
- May 03, 2006
internals@lists.php.net/msg21617.html" rel="nofollow" target="_blank">http://www.mail-archive.com/internals@lists.php.net/msg21617.html
Maybe I am then misunderstanding your proposal, as to me it is clearly covered and deemed not possible:
http://wiki.php.net/rfc/ifsetor#rejected_features
$var = ifsetor($var, $var2, "admin");
However this is currently not possible to be implemented without major slowdowns to the engine.
regards,
Lukas Kahwe Smith
mls@pooteeweet.org
Le Sat, 21 Nov 2009 10:21:18 -0800, Rasmus Lerdorf a écrit :
The ternary isn't meant to solve the isset thing you are talking about.
It is simply a shortcut to normal ternary operations. The most common
case where you don't know if a variable is set is on the initial input
via $_GET or $_POST and we definitely don't want people doing:$var = $_GET['foo'] ?: 42;
It would be an XSS disaster. Hence the suggestion to use input_filter
there, or a similar user-supplied filtering function in which case the
ternary, as it is currently implemented, is perfectly suitable.-Rasmus
Sure ! Developpers should filter variables contents !
Generaly there are 3 step for treat incoming variable :
1- checking existance of the variable.
2- set a default value if it not exists or empty.
3- filtering the variable content.
Generaly, we combine step 1 and 2 in one.
I don't recommand using empty() because this method has some side effect
like '0' or 'off' which are interpreted as empty values. I prefer use
isset() method.
Every time, we need to check existance of variable. Checking if var is
empty to fill it with a default value is optionnal, this step differs
depending on the program behaviour.
For the third step, filter extension for example are perfect.
So, PHP provides an excellent short syntax to fill empty variable with
the ternary operator.
But there is a lack for the most common case which is check if variable
is set.
Perhaps I've made a mistake by suggesting a ternary operator improvement.
The real needing is :
I want a pretty and short syntax like the ternary operator for checking
if a variable is set and set a default value if it not set.
I have one for array with union opertor.
$_POST += array(
'foo' => '',
'bar' => '',
'baz' => '',
);
And having something like that would be nice :
$var ?= 'default';
// this can work too for array
$_GET['foo'] ?= 'default';
I am aware that I am repeating myself and that I seem insistant. It is
difficult to express ideas clearly when one is not very comfortable with
English and I apologize for that :)
--
Alban Leroux seza@paradoxal.org
Alban wrote:
Le Sat, 21 Nov 2009 10:21:18 -0800, Rasmus Lerdorf a écrit :
The ternary isn't meant to solve the isset thing you are talking about.
It is simply a shortcut to normal ternary operations. The most common
case where you don't know if a variable is set is on the initial input
via $_GET or $_POST and we definitely don't want people doing:$var = $_GET['foo'] ?: 42;
It would be an XSS disaster. Hence the suggestion to use input_filter
there, or a similar user-supplied filtering function in which case the
ternary, as it is currently implemented, is perfectly suitable.-Rasmus
Sure ! Developpers should filter variables contents !
Generaly there are 3 step for treat incoming variable :
1- checking existance of the variable.
2- set a default value if it not exists or empty.
3- filtering the variable content.
Or better yet, have your filter function return false if the variable
doesn't exist and use the ternary to set the default. You can do it all
in a single step then.
$var = filter_func($_GET,'foo')?:42;
Simple and clean.
-Rasmus
Le Sat, 21 Nov 2009 19:52:30 -0800, Rasmus Lerdorf a écrit :
Or better yet, have your filter function return false if the variable
doesn't exist and use the ternary to set the default. You can do it all
in a single step then.$var = filter_func($_GET,'foo')?:42;
Simple and clean.
-Rasmus
Yes, as you say, simple and clean !
Have I miss something like that in php ?
--
Alban Leroux