# New Operator

- Thread: https://externals.io/message/22388
- Original archive: https://news-web.php.net/php.internals/22388
- Started by Jason Garber on 2006-03-14T01:30:35+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #22388 — Jason Garber — 2006-03-14T01:30:35+00:00

URL: https://externals.io/message/22388#22388

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.

---

## #22408 — Cristiano Duarte — 2006-03-14T14:24:52+00:00

URL: https://externals.io/message/22388#22408 · In reply to: #22388

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.

---

## #22412 — Brian Moon — 2006-03-14T15:06:33+00:00

URL: https://externals.io/message/22388#22412 · In reply to: #22408

> > […]
> 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

---

## #22427 — Cristiano Duarte — 2006-03-14T20:55:05+00:00

URL: https://externals.io/message/22388#22427 · In reply to: #22412

Brian Moon wrote:

> > […]
> 
> 
> 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

---

## #22433 — Brian Moon — 2006-03-14T21:36:36+00:00

URL: https://externals.io/message/22388#22433 · In reply to: #22427

> 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.

---

## #22434 — alpha@rrs@Nico Edtinger — 2006-03-14T22:43:56+00:00

URL: https://externals.io/message/22388#22434 · In reply to: #22433

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

---

## #22450 — bertrand Gugger — 2006-03-15T08:18:52+00:00

URL: https://externals.io/message/22388#22450 · In reply to: #22433

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
