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
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 anE_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 becauseTRUE
will
then be assigned to the new variable if the expression evaluates toTRUE
(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 theE_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
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
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'.
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. |
`------------------------------------------------------------'
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
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
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
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.
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.
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 anE_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 becauseTRUE
will
then be assigned to the new variable if the expression evaluates toTRUE
(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 theE_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
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).
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.hi,
i just found the new ifsetor like construction in php6. It left me the
question why php has to throw anE_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 becauseTRUE
will
then be assigned to the new variable if the expression evaluates toTRUE
(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 theE_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
--
--
Etienne Kneuss
http://www.colder.ch
Men never do evil so completely and cheerfully as
when they do it from a religious conviction.
-- Pascal
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.
hi,
i just found the new ifsetor like construction in php6. It left me the
question why php has to throw anE_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 becauseTRUE
will
then be assigned to the new variable if the expression evaluates toTRUE
(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 theE_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
Best regards,
Marcus
hi,
i just found the new ifsetor like construction in php6.
(FYI: It has already been merged to 5.3)
-Hannes