Re: suggestion about ternary operator

From: Date: Sun, 22 Nov 2009 03:52:30 +0000
Subject: Re: suggestion about ternary operator
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-46154@lists.php.net to get a copy of this message
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

« previous php.internals (#46154) next »