New Operator

php.internals

Jason Garber

20 years ago
Hello internals, Since nobody could seem to agree on a language construct for accessing a variable that you don't know exists (ifsetor), I would like to propose a simpler approach. It returns the value of the passed variable, or NULL if the variable does not exist. ifset($some_array['some-key-you-are-not-sure-exists']); This E_STRICT ternary pain-in-the-ass expression is constantly at the top of every PHP script I write: $ConfirmCode = (isset($_GET['ConfirmCode']) ? $_GET['ConfirmCode'] : ''); Please?
-- Best regards, Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.

Cristiano Duarte

20 years ago
Jason Garber wrote:
> Hello internals, > > Since nobody could seem to agree on a language construct for > accessing a variable that you don't know exists (ifsetor), > I would like to propose a simpler approach. > > It returns the value of the passed variable, or NULL if the variable > does not exist. > > ifset($some_array['some-key-you-are-not-sure-exists']);
IMHO, this wouldn't solve the problem. The problem is that we need default values, not NULL. Sometimes you may want NULL sometimes you may not.
> > This E_STRICT ternary pain-in-the-ass expression is constantly at > the top of every PHP script I write: > > $ConfirmCode = (isset($_GET['ConfirmCode']) ? $_GET['ConfirmCode'] : > '');
I agree that this is really a PITA, but until we get an operator for this, we should stuck on the ternary.

Brian Moon

20 years ago
>> $ConfirmCode = (isset($_GET['ConfirmCode']) ? $_GET['ConfirmCode'] : >> ''); > I agree that this is really a PITA, but until we get an operator for this, > we should stuck on the ternary.
Another option is to use settype(); This has two advantages. One, if the variable does not exist, settype creates it with an empty value of the given type. Second, if it does exist, it can semitypecast them for you. settype($_GET['ConfirmCode'], "string"); or $ConfirmCode = $_GET["ConfirmCode"]; settype($ConfirmCode, "string"); Brian Moon dealnews.com

Cristiano Duarte

20 years ago
Brian Moon wrote:
>>> $ConfirmCode = (isset($_GET['ConfirmCode']) ? $_GET['ConfirmCode'] : >>> ''); >> I agree that this is really a PITA, but until we get an operator for >> this, we should stuck on the ternary. > > > Another option is to use settype(); This has two advantages. One, if > the variable does not exist, settype creates it with an empty value of > the given type. Second, if it does exist, it can semitypecast them for > you. > > settype($_GET['ConfirmCode'], "string"); > > or > > $ConfirmCode = $_GET["ConfirmCode"]; > settype($ConfirmCode, "string");
But you still don't have a default value, just an empty default value(string, zero, etc.). You can't i.e. initialize $_GET["ConfirmCode"] with "Yes" using settype... Regards, Cristiano Duarte

Brian Moon

20 years ago
> But you still don't have a default value, just an empty default > value(string, zero, etc.). You can't i.e. initialize $_GET["ConfirmCode"] > with "Yes" using settype...
Oh, you want an operator to do that? Does some other language have this that we are wanting to copy here? Seems overly complicated to me. But, so does a lot of things being added PHP these days. Brian.

Nico Edtinger

20 years ago
Brian Moon wrote:
> Oh, you want an operator to do that? Does some other language have > this that we are wanting to copy here?
IIRC C# 3.0 adds a new operator ?: written as "foo = bar ?: 'default value';". I tried to search a resource with some documentation about this new operator, but Google can't search for "?:" and live.com is dead. BTW the same operator can be found in Derick's meeting notes for PHP 6.0: http://www.php.net/~derick/meeting-notes.html#ifsetor-as- replacement-for-foo-isset-foo-foo-something-else nico

bertrand Gugger

20 years ago
Brian Moon wrote:
> Oh, you want an operator to do that? Does some other language have this > that we are wanting to copy here? Seems overly complicated to me. But, > so does a lot of things being added PHP these days.
As bash ${parameter:=word} ?
-- toggg