# The constant use of isset()

- Thread: https://externals.io/message/43902
- Original archive: https://news-web.php.net/php.internals/43902
- Started by olafurw@gmail.com on 2009-05-12T16:35:27+00:00
- Messages: 11
- 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

---

## #43902 — olafurw@gmail.com — 2009-05-12T16:35:27+00:00

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

While researching for this suggestion I found this rfc proposal regarding
ifsetor() ( http://wiki.php.net/rfc/ifsetor?s[]=isset<http://wiki.php.net/rfc/ifsetor?s%5B%5D=isset>)
and it's rejection point was that it was currently not possible (
http://marc.info/?l=php-internals&m=108931281901389&w=2 )

But would it be possible to check for a value of a variable if it is set?

Since I often do (and see others do)

if(isset($_GET["foo"]) && $_GET["foo"] == "bar")
or even worse
if((isset($_GET["foo"]) && $_GET["foo"] == "bar") || (isset($_GET["baz"]) &&
$_GET["baz"] == "bat"))

to be able to do something like this

if(isset($_GET["foo"]) == "bar")
or
if(isset($_GET["foo"]) == "bar" || isset($_GET["baz"]) == "bat")

That isset (or some other language construct) would return the variable if
it were set and false if it was not.

Thanks for your time, i know this has probably been talked to death in one
form or other.

Ólafur Waage
olafurw@gmail.com

---

## #43904 — Sean Coates — 2009-05-12T16:51:36+00:00

URL: https://externals.io/message/43902#43904

> So if the variable is set and contains "false", we can't check it ?
> And near same problem for 0, empty array and empty string.

How would you ever get false (the value, not the string) into a  
request variable? (without setting it that way after the request init,  
that is)

S

---

## #43905 — olafurw@gmail.com — 2009-05-12T16:51:51+00:00

URL: https://externals.io/message/43902#43905

The error suppression was discussed in the rfc and yes it is not clean and
you could be suppressing something else inadvertently.

Yes the false value would be an issue with this, but for 0, empty array and
empty string is an issue with just about anything else in PHP already. Hence
=== if you want to be strict with it.

2009/5/12 Olivier B. <php-dev.list@daevel.net>

> So if the variable is set and contains "false", we can't check it ?
> And near same problem for 0, empty array and empty string.
>
> But you can also use this syntax : (yes it's not very "clean")
>
>   if( @$_GET['foo'] === 'bar')
>   or
>   if( @$_GET['foo'] === 'bar' or @$_GET['baz'] === 'bat' )
>
>
> Olivier
>
> Ólafur Waage a écrit :
>
> > […]

---

## #43907 — Sean Coates — 2009-05-12T17:09:04+00:00

URL: https://externals.io/message/43902#43907

> So this isset() behavior will only be available for request  
> variable, not for all variables ?

I'm not picking sides, but that seems to me like the overwhelmingly  
popular use case.

S

---

## #43908 — Lewis Wright — 2009-05-12T17:23:47+00:00

URL: https://externals.io/message/43902#43908 · In reply to: #43902

Regarding ifsetor, what's wrong with just using this:

isset($myvar) OR $myvar = 'i am set';

It works in just the same way and has no problems. I agree it would be
great though if there could be a function to retrieve a variable's
value if it exists, without throwing an error if it doesn't exist. I'm
not sure if isset would be appropriate though.

Lewis.

2009/5/12 Ólafur Waage <olafurw@gmail.com>:
> While researching for this suggestion I found this rfc proposal regarding
> ifsetor() ( http://wiki.php.net/rfc/ifsetor?s[]=isset<http://wiki.php.net/rfc/ifsetor?s%5B%5D=isset>)
> and it's rejection point was that it was currently not possible (
> http://marc.info/?l=php-internals&m=108931281901389&w=2 )
>
> But would it be possible to check for a value of a variable if it is set?
>
> Since I often do (and see others do)
>
> if(isset($_GET["foo"]) && $_GET["foo"] == "bar")
> or even worse
> if((isset($_GET["foo"]) && $_GET["foo"] == "bar") || (isset($_GET["baz"]) &&
> $_GET["baz"] == "bat"))
>
> to be able to do something like this
>
> if(isset($_GET["foo"]) == "bar")
> or
> if(isset($_GET["foo"]) == "bar" || isset($_GET["baz"]) == "bat")
>
> That isset (or some other language construct) would return the variable if
> it were set and false if it was not.
>
> Thanks for your time, i know this has probably been talked to death in one
> form or other.
>
> Ólafur Waage
> olafurw@gmail.com

---

## #43910 — Robert Cummings — 2009-05-12T17:39:41+00:00

URL: https://externals.io/message/43902#43910 · In reply to: #43908

> Regarding ifsetor, what's wrong with just using this:
> 
> isset($myvar) OR $myvar = 'i am set';
> 
> It works in just the same way and has no problems. I agree it would be
> great though if there could be a function to retrieve a variable's
> value if it exists, without throwing an error if it doesn't exist. I'm
> not sure if isset would be appropriate though.
> 
> Lewis.

mixed value_of( $variable [, $defaultValue=null] );

<?php

if( value_of( $_GET['action'] ) == 'edit' )
{
    // ...
}

switch( value_of( $_GET['action'], 'details' ) )
{
    case 'details': ...
}

?>

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP

---

## #43915 — Jared Williams — 2009-05-12T19:15:24+00:00

URL: https://externals.io/message/43902#43915 · In reply to: #43902

> -----Original Message-----
> From: Ólafur Waage [mailto:olafurw@gmail.com] 
> Sent: 12 May 2009 17:35
> To: internals@lists.php.net
> Subject: [PHP-DEV] The constant use of isset()
> 
> While researching for this suggestion I found this rfc 
> proposal regarding
> ifsetor() ( 
> http://wiki.php.net/rfc/ifsetor?s[]=isset<http://wiki.php.net/
> rfc/ifsetor?s%5B%5D=isset>)
> and it's rejection point was that it was currently not possible (
> http://marc.info/?l=php-internals&m=108931281901389&w=2 )
> 
> But would it be possible to check for a value of a variable 
> if it is set?
> 
> Since I often do (and see others do)
> 
> if(isset($_GET["foo"]) && $_GET["foo"] == "bar") or even worse
> if((isset($_GET["foo"]) && $_GET["foo"] == "bar") || 
> (isset($_GET["baz"]) && $_GET["baz"] == "bat"))
> 
> to be able to do something like this
> 
> if(isset($_GET["foo"]) == "bar")
> or
> if(isset($_GET["foo"]) == "bar" || isset($_GET["baz"]) == "bat")
> 
> That isset (or some other language construct) would return 
> the variable if it were set and false if it was not.
> 
> Thanks for your time, i know this has probably been talked to 
> death in one form or other.
> 
> Ólafur Waage
> olafurw@gmail.com
> 

Use array_merge to provide default values... 

$get = array_merge($_GET, array('foo' => 'bar', 'baz' => 'bat));

If ($get['foo'] == 'bar' || $get['baz'] == 'bat')

Jared

---

## #43916 — olafurw@gmail.com — 2009-05-12T19:35:48+00:00

URL: https://externals.io/message/43902#43916 · In reply to: #43910

On Tue, May 12, 2009 at 5:39 PM, Robert Cummings <robert@interjinn.com>wrote:

> 
> > […]
>
> mixed value_of( $variable [, $defaultValue=null] );
>
> <?php
>
> if( value_of( $_GET['action'] ) == 'edit' )
> {
>    // ...
> }
>
> switch( value_of( $_GET['action'], 'details' ) )
> {
>    case 'details': ...
> }
>
> ?>
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
>

Will this run without notices if the value you are passing to the function
isnt set?

Olafur

---

## #43917 — Robert Cummings — 2009-05-12T19:51:38+00:00

URL: https://externals.io/message/43902#43917 · In reply to: #43916

> On Tue, May 12, 2009 at 5:39 PM, Robert Cummings <robert@interjinn.com>wrote:
> 
> > […]
> 
> Will this run without notices if the value you are passing to the function
> isnt set?

No, I was suggesting a format for the desired functionality (I should
have been more clear). IMHO the above would be the perfect solution if
it worked in the same manner as isset() (i.e. doesn't generate notices).

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP

---

## #43918 — olafurw@gmail.com — 2009-05-12T21:13:40+00:00

URL: https://externals.io/message/43902#43918

2009/5/12 Brian Moon <brian@moonspot.net>

> $foo = filter_input(INPUT_GET, "foo", FILTER_UNSAFE_RAW);
>
> That would have a value if set or null if not set.  It also allows you to
> validate it using filters if you wanted to.  This of course only works with
> GPC variables, but it is a great solution.
>
> Brian.
> --------
> http://brian.moonspot.net/


Can this be turned into a userland function?

Olafur


>
>
> 
>
> > […]

---

## #43920 — olafurw@gmail.com — 2009-05-13T09:14:06+00:00

URL: https://externals.io/message/43902#43920

2009/5/12 Ionut Gabriel Stan <ionut.g.stan@gmail.com>

> 2009/5/13 Ólafur Waage <olafurw@gmail.com>:
> > […]
> to
> > […]
> with
> > […]
>
> If you're interested in userland code, then you have two solutions:
> 1. Make functions like GET, POST, COOKIE, SESSION and use them instead
> of the superglobals (functions are superglobal). An example:
> ----------------------------------------------------------------
>
> // PHP < 5.3
> function GET($key, $default = null) {
>    return isset($_GET[$key]) ? $_GET[$key] : $default;
> }
>
> // PHP >= 5.3
> function GET($key, $default = null) {
>    return isset($_GET[$key]) ?: $default;
> }
>
>
> 2. Use ArrayObject to override the superglobals in an auto_prepend_file:
> -----------------------------------------------------------------
>
> $_GET = ArrayObject($_GET);
>
>
>
> Anyway, these will save the trouble with the superglobals, not with
> ordinary arrays.
>

Yes this will work with GPC variables but not with any other variable. And
passing values to it even feels right.

if(GET("foo") == "bar")


>
>
> > […]
> regarding
> > […]
> <
> > […]
> set?
> > […]
> if
> > […]
> one
> > […]
>
>
>
> --
> Ionut G. Stan
> I'm under construction  |  http://igstan.blogspot.com/
