# ifsetor like expression in php6

- Thread: https://externals.io/message/34939
- Original archive: https://news-web.php.net/php.internals/34939
- Started by Sebastian on 2008-01-26T16:30:32+00:00
- Messages: 13
- 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

---

## #34939 — Sebastian — 2008-01-26T16:30:32+00:00

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

hi,

i just found the new ifsetor like construction in php6. It left me the 
question why php has to throw an `E_NOTICE` when the variable is not set. This 
is basicly because that actually is the meaning of this construction, 
checking if the variable is set and if it isnt give back a standard value. 
Of course i know this construction can be (mis)used with an Boolean check 
($var===$var2), but this does not make much sence either because `TRUE` will 
then be assigned to the new variable if the expression evaluates to `TRUE` 
(but please correct me if theres a real use for this).

So in any case it would be better to remove the Boolean check feature and 
make it a simple ifsetor() without the `E_NOTICE` again.

For those of you who have not heard of it heres an example:

<?php

// If $_GET['foo'] is set, then its value will be assigned to $foo,
// otherwise 42 will be assigned to $foo.

$foo = $_GET['foo'] ?: 42;

?>

Greetings

Sebastian

---

## #34940 — Sam Barrow — 2008-01-26T17:17:24+00:00

URL: https://externals.io/message/34939#34940 · In reply to: #34939

I don't think throwing a `E_NOTICE` is appropriate. The isset() construct
doesn't throw an E_NOTICE, this shouldn't either.


> hi,
> 
> i just found the new ifsetor like construction in php6. It left me the 
> question why php has to throw an `E_NOTICE` when the variable is not set. This 
> is basicly because that actually is the meaning of this construction, 
> checking if the variable is set and if it isnt give back a standard value. 
> Of course i know this construction can be (mis)used with an Boolean check 
> ($var===$var2), but this does not make much sence either because `TRUE` will 
> then be assigned to the new variable if the expression evaluates to `TRUE` 
> (but please correct me if theres a real use for this).
> 
> So in any case it would be better to remove the Boolean check feature and 
> make it a simple ifsetor() without the `E_NOTICE` again.
> 
> For those of you who have not heard of it heres an example:
> 
> <?php
> 
> // If $_GET['foo'] is set, then its value will be assigned to $foo,
> // otherwise 42 will be assigned to $foo.
> 
> $foo = $_GET['foo'] ?: 42;
> 
> ?>
> 
> Greetings
> 
> Sebastian

---

## #34941 — Sam Barrow — 2008-01-26T17:22:09+00:00

URL: https://externals.io/message/34939#34941 · In reply to: #34939

I'm sorry I misunderstood. If specifying like "$var ?: 5" then it should
throw an E_NOTICE, as this is a conditional that checks the value of a
variable ($var).

Sebastian, for assigning of a default value is a variable is not set, I
would recommend using something like this (this is what I use):

function ifsetor(&$var, $or) {
	return isset($var) ? $var : $or ;
}

Because $var is passed by reference, it won't throw an `E_NOTICE` when
passing an undeclared variable.


> hi,
> 
> i just found the new ifsetor like construction in php6. It left me the 
> question why php has to throw an `E_NOTICE` when the variable is not set. This 
> is basicly because that actually is the meaning of this construction, 
> checking if the variable is set and if it isnt give back a standard value. 
> Of course i know this construction can be (mis)used with an Boolean check 
> ($var===$var2), but this does not make much sence either because `TRUE` will 
> then be assigned to the new variable if the expression evaluates to `TRUE` 
> (but please correct me if theres a real use for this).
> 
> So in any case it would be better to remove the Boolean check feature and 
> make it a simple ifsetor() without the `E_NOTICE` again.
> 
> For those of you who have not heard of it heres an example:
> 
> <?php
> 
> // If $_GET['foo'] is set, then its value will be assigned to $foo,
> // otherwise 42 will be assigned to $foo.
> 
> $foo = $_GET['foo'] ?: 42;
> 
> ?>
> 
> Greetings
> 
> Sebastian

---

## #34942 — Stefan Walk — 2008-01-26T18:17:52+00:00

URL: https://externals.io/message/34939#34942 · In reply to: #34941

On Saturday 26 January 2008 18:22:09 Sam Barrow wrote:
> I'm sorry I misunderstood. If specifying like "$var ?: 5" then it should
> throw an E_NOTICE, as this is a conditional that checks the value of a
> variable ($var).
>
> Sebastian, for assigning of a default value is a variable is not set, I
> would recommend using something like this (this is what I use):
>
> function ifsetor(&$var, $or) {
> 	return isset($var) ? $var : $or ;
> }

That magically brings $foo into life if you do ifsetor($foo, 2).

---

## #34943 — Etienne Kneuss — 2008-01-26T18:18:51+00:00

URL: https://externals.io/message/34939#34943 · In reply to: #34941

Hi,
usage of references here have some bad consequences, it will pollute
the input array with null entries:

function foo(&$a) { }
var_dump(array_key_exists('inexistent', $_GET)); // bool(false)
foo($_GET['inexistent']);
var_dump(array_key_exists('inexistent', $_GET)); // bool(true)

will actually create the inexistent entry and fill it with null.
Again, there is no perfect implementation of an issetor in userland.


> I'm sorry I misunderstood. If specifying like "$var ?: 5" then it should
> throw an E_NOTICE, as this is a conditional that checks the value of a
> variable ($var).
>
> Sebastian, for assigning of a default value is a variable is not set, I
> would recommend using something like this (this is what I use):
>
> function ifsetor(&$var, $or) {
>         return isset($var) ? $var : $or ;
> }
>
> Because $var is passed by reference, it won't throw an `E_NOTICE` when
> passing an undeclared variable.
>
> 
>
> > […]
>
> --
> 
> 
>
>
>



-- 
Etienne Kneuss
http://www.colder.ch

Men never do evil so completely and cheerfully as
when they do it from a religious conviction.
-- Pascal

---

## #34944 — Hannes Magnusson — 2008-01-26T18:19:45+00:00

URL: https://externals.io/message/34939#34944 · In reply to: #34939

> hi,
>
> i just found the new ifsetor like construction in php6.

(FYI: It has already been merged to 5.3)

-Hannes

---

## #34945 — Lars Strojny — 2008-01-26T21:13:36+00:00

URL: https://externals.io/message/34939#34945 · In reply to: #34940

Hi,

Am Samstag, den 26.01.2008, 12:17 -0500 schrieb Sam Barrow:
> I don't think throwing a `E_NOTICE` is appropriate. The isset() construct
> doesn't throw an E_NOTICE, this shouldn't either.

As far as I understand it is just an extension to the already present
tertiary operator and therefore the ifsetor would be
isset($_GET['foo']) ?: 'bar'. I don't understand why that sugar should
act anything different and not trigger an E_NOTICE.

cu, Lars
-- 
  »Aber das Verhältnis von Leben und Produktion, das jenes
   real herabsetzt zur ephemeren Erscheinung von dieser, ist
   vollendet widersinnig. Mittel und Zweck werden vertauscht.«
       -- Theodor W. Adorno, »Minima Moralia«, Die traurige Wissenschaft

Lars Strojny
Nießenstr. 36
51103 Cologne
Jabber/Mail: lars@strojny.net
Weblog: http://usrportage.de

---

## #34946 — Robert Cummings — 2008-01-26T21:23:51+00:00

URL: https://externals.io/message/34939#34946 · In reply to: #34945

> Hi,
> 
> Am Samstag, den 26.01.2008, 12:17 -0500 schrieb Sam Barrow:
> > […]
> 
> As far as I understand it is just an extension to the already present
> tertiary operator and therefore the ifsetor would be
> isset($_GET['foo']) ?: 'bar'.

This doesn't work how you think... if $_GET['foo'] is set then the
return value is true and not the value of $_GET['foo'].

>  I don't understand why that sugar should
> act anything different and not trigger an E_NOTICE.

I thought it was supposed to be a shortcut for doing the following:

    $foo = isset( $_GET['foo'] ) ? $_GET['foo'] : 'xyz';

And I thought the whole point was for it to not generate a notice. Isn't
that why the name ifsetor was chosen? Since it's supposed to work like
isset() by not generating notices?

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

---

## #34947 — johannes@php.net — 2008-01-26T21:43:34+00:00

URL: https://externals.io/message/34939#34947 · In reply to: #34946

Hi,


> And I thought the whole point was for it to not generate a notice. Isn't
> that why the name ifsetor was chosen? Since it's supposed to work like
> isset() by not generating notices?

This thing is not called "ifsetor". From the commit message:

 ?: operator
 [DOC] "expr1 ?: expr1" is a shortcut for: "expr1
 ? expr1 : expr2" as exists in gcc and discussed some time back. Note
 that this is not an implementation ifsetor($var, default). While
 ifsetor would not generate any message for non existing variables or
 array indices the ternary shortcut does. Also the ternary shortcut does
 a boolean evaluation rather then checking for isset(). That way ther
 ternary shortcut can work on any expression while ifsetor can only work
 on variables. Also to be silent one has do do: "@$expr1 ?: $expr2".

I didn't check the docs, if it is documented as "ifsetor" there that's
wrong.

For reasons why there is no "ifsetor" but this operator please check the
archives.

johannes

---

## #34948 — Lars Strojny — 2008-01-26T22:10:47+00:00

URL: https://externals.io/message/34939#34948 · In reply to: #34946

Hi,

Am Samstag, den 26.01.2008, 16:23 -0500 schrieb Robert Cummings:
[...]
> This doesn't work how you think... if $_GET['foo'] is set then the
> return value is true and not the value of $_GET['foo'].

Hm, check what happens:

$b = array(false, false);
var_dump($b[0] ?: "sdf");

This will return "sdf", not bool(false).

cu, Lars
-- 
  »Aber das Verhältnis von Leben und Produktion, das jenes
   real herabsetzt zur ephemeren Erscheinung von dieser, ist
   vollendet widersinnig. Mittel und Zweck werden vertauscht.«
       -- Theodor W. Adorno, »Minima Moralia«, Die traurige Wissenschaft

Lars Strojny
Nießenstr. 36
51103 Cologne
Jabber/Mail: lars@strojny.net
Weblog: http://usrportage.de

---

## #34949 — Lars Strojny — 2008-01-26T22:12:40+00:00

URL: https://externals.io/message/34939#34949 · In reply to: #34947

Hi,

Am Samstag, den 26.01.2008, 22:43 +0100 schrieb Johannes Schlüter:
[...]
>  ?: operator
>  [DOC] "expr1 ?: expr1" is a shortcut for: "expr1
>  ? expr1 : expr2" as exists in gcc and discussed some time back. Note
>  that this is not an implementation ifsetor($var, default). While
>  ifsetor would not generate any message for non existing variables or
>  array indices the ternary shortcut does. Also the ternary shortcut does
>  a boolean evaluation rather then checking for isset(). That way ther
>  ternary shortcut can work on any expression while ifsetor can only work
>  on variables. Also to be silent one has do do: "@$expr1 ?: $expr2".

Thanks for making this clear. That's exactly how I understood it and I
didn't read before about it. That's why I guess it is intuitive :-)

cu, Lars

---

## #34950 — Sam Barrow — 2008-01-26T22:31:10+00:00

URL: https://externals.io/message/34939#34950 · In reply to: #34945

> Hi,
> 
> Am Samstag, den 26.01.2008, 12:17 -0500 schrieb Sam Barrow:
> > […]
> 
> As far as I understand it is just an extension to the already present
> tertiary operator and therefore the ifsetor would be
> isset($_GET['foo']) ?: 'bar'. I don't understand why that sugar should
> act anything different and not trigger an E_NOTICE.

Yes, you're right. I misunderstood this to mean isset($foo) ? $foo :
$default. I didn't realize that Sebastian was referring to the new
shortcut for the ternary operator.

---

## #34954 — Marcus Boerger — 2008-01-27T11:13:27+00:00

URL: https://externals.io/message/34939#34954 · In reply to: #34941

Hello Sam,

  as others pointed out & results in unwanted entries being present. So the
second time you use ifsetor on those entries, you do not get the default
back but rather NULL. Actually in your implementation you never get the
default back.

Please read the archives, all of this has been discussed several times and
we have had a bunch of in depth explanations. In fact our archives hold a
lot of information valid to people new to the list as well as for us
following the list for years. I guess I am not the only one having my own
searchable archive always with me - for a reason.

marcus

Saturday, January 26, 2008, 6:22:09 PM, you wrote:

> I'm sorry I misunderstood. If specifying like "$var ?: 5" then it should
> throw an E_NOTICE, as this is a conditional that checks the value of a
> variable ($var).

> Sebastian, for assigning of a default value is a variable is not set, I
> would recommend using something like this (this is what I use):

> function ifsetor(&$var, $or) {
>         return isset($var) ? $var : $or ;
> }

> Because $var is passed by reference, it won't throw an `E_NOTICE` when
> passing an undeclared variable.

> 
> > […]




Best regards,
 Marcus
