Hi all,
I was surprised that wired min()
behavior.
The cause of this behavior is in Zend API.
https://bugs.php.net/bug.php?id=53104
So I've made FRC to discuss/document/fix such behavior.
If anyone know such behavior, please let me know so that I can write them
in the RFC.
Thank you.
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Hi!
I was surprised that wired
min()
behavior.
The cause of this behavior is in Zend API.https://bugs.php.net/bug.php?id=53104
So I've made FRC to discuss/document/fix such behavior.
If anyone know such behavior, please let me know so that I can write them
in the RFC.
In the engine, null is less than -1. Changing it would be a very
profound compatibility break, so I do not think it can be done in any
5.x version.
Current behavior is described here in detail:
http://php.net/manual/en/language.operators.comparison.php
Null is the same as false in this case, which is less than any non-false
value. -1 is a non-false value.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi Stas,
On Sat, Nov 9, 2013 at 5:28 PM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Hi!
I was surprised that wired
min()
behavior.
The cause of this behavior is in Zend API.https://bugs.php.net/bug.php?id=53104
So I've made FRC to discuss/document/fix such behavior.
If anyone know such behavior, please let me know so that I can write them
in the RFC.In the engine, null is less than -1. Changing it would be a very
profound compatibility break, so I do not think it can be done in any
5.x version.Current behavior is described here in detail:
http://php.net/manual/en/language.operators.comparison.phpNull is the same as false in this case, which is less than any non-false
value. -1 is a non-false value.
My intention is not fix them all, but document them if we are not
going to fix inconsistent behaviors.
For example, string may be converted to int when it seems int/float or hex,
but it's not converted when it seems octal or binary. I think it's not
documented yet. (At least not clearly, is it? Adding octal/binary conversion
is not a good idea. Likewise, -1 should be true.)
I'm wandering whether there are functions like min()
. It does not
follow basic comparison/conversion rules. min()
behavior is nonsense and
it would be good fixing min()
at some point. Not many users will be
affected, IMO.
https://bugs.php.net/bug.php?id=53104
There may be function like min()
, but it may not be feasible to change
behavior. If change is not feasible, we may document behavior instead.
If we fix little inconsistencies like this, it would be better fix them all
at once.
Do you know any functions like min()
?
Regards,
BTW, typo in s/wired/weird/ in first post :(
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Hi!
For example, string may be converted to int when it seems int/float or hex,
but it's not converted when it seems octal or binary. I think it's not
documented yet. (At least not clearly, is it? Adding octal/binary conversion
is not a good idea. Likewise, -1 should be true.)
0x conversion is kind of documented in
http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
by referring to strtod. But I agree that it might as well spell it out
there without sending people to manpages.
As for the rest, I'm not sure what we can document there besides the
fact numbers are decimal, which is kind of obvious. This is certainly
not "inconsistent behavior" - this is a behavior which is consistent and
documented, just it refers to other place, which can be improved.
I'm wandering whether there are functions like
min()
. It does not
I would assume anything that does standard comparisons would be working
the same. Doing otherwise would be, yes, inconsistent. So any function
calling compare_function would do the same thing min()
does, I imagine.
follow basic comparison/conversion rules.
min()
behavior is nonsense and
In which case does min()
not follow comparison rules as outlined in the
manual?
it would be good fixing
min()
at some point. Not many users will be
affected, IMO.
I think this opinion is wrong, and I don't see why these rules are
"nonsense". Some of these rules may not produce result you have
expected, but only because you are comparing types between which natural
comparison does not exist, so we have to choose some behavior which is
arbitrary to a measure. If you look at Python, for example, "None < -1"
is true (or, if you will, True). In Javascript, however, "null < -1" is
false. Both choices have reasons.
Changing basic comparison rules in the language has very high potential
of affecting users, since virtually every script does comparisons. Of
course, you don't know if anybody ever compares to nulls, but that's not
impossible, so we need to be very careful.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi Stas,
On Sun, Nov 10, 2013 at 4:54 PM, Stas Malyshev smalyshev@sugarcrm.comwrote:
I'm wandering whether there are functions like
min()
. It does notI would assume anything that does standard comparisons would be working
the same. Doing otherwise would be, yes, inconsistent. So any function
calling compare_function would do the same thingmin()
does, I imagine.follow basic comparison/conversion rules.
min()
behavior is nonsense andIn which case does
min()
not follow comparison rules as outlined in the
manual?
Our manual does not have NULL/BOOL type behavior explanations.
it would be good fixing
min()
at some point. Not many users will be
affected, IMO.I think this opinion is wrong, and I don't see why these rules are
"nonsense". Some of these rules may not produce result you have
expected, but only because you are comparing types between which natural
comparison does not exist, so we have to choose some behavior which is
arbitrary to a measure. If you look at Python, for example, "None < -1"
is true (or, if you will, True). In Javascript, however, "null < -1" is
false. Both choices have reasons.Changing basic comparison rules in the language has very high potential
of affecting users, since virtually every script does comparisons. Of
course, you don't know if anybody ever compares to nulls, but that's not
impossible, so we need to be very careful.
I agree your point fully.
I think almost all users are using min()
only with integer/float and not
with bool/null.
The bug report might be confusing, but it's is not complaining -1 != false
or like, but it's complaining min()
is not following standard PHP
comparison rules.
if (-1 < NULL)
echo 'NULL is smaller';
else
echo '-1 is smaller';
returns '-1 is smaller' as all of us expects.
However,
min(-1,null)
returns NULL.
NULL
is evaluated as 0 in scalar contexts, but not with min()
. NULL
wins
negative values because of the compare_function() in zend_operators.c.
compare_function() treats NULL/BOOL type special way. (i.e Not like
expression in 'if' statement or like)
That's the reason why I was surprised.
I think feasible solution for min()
misbehavior is to use new function that
uses standard PHP comparison rule. I'll check PHP functions that uses
compare_function() later. There may be other array functions use it. I'm
not sure if there are 'internal' functions that does not perform
comparison(or conversion) non standard way like compare_function().
It's a small change, but it might have big impact for some users.
Therefore, if I'll propose this RFC as PHP 6 matter.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
if (-1 < NULL)
echo 'NULL is smaller';
else
echo '-1 is smaller';returns '-1 is smaller' as all of us expects.
Please go over the conditions and the echos in this example one more
time.
-- S.
Hi Sanford,
On Sun, Nov 10, 2013 at 6:07 PM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
if (-1 < NULL)
echo 'NULL is smaller';
else
echo '-1 is smaller';returns '-1 is smaller' as all of us expects.
Please go over the conditions and the echos in this example one more
time.
I think I understand your argument as I know languages that treat null/nil
as special value.
I just think
min(-10000, -1000, -100, NULL, 100, 1000) returns NULL.
is not following standard PHP comparison rules.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
if (-1 < NULL)
echo 'NULL is smaller';
else
echo '-1 is smaller';returns '-1 is smaller' as all of us expects.
Please go over the conditions and the echos in this example one more
time.
I think I understand your argument as I know languages that treat null/nil as special value.
I just think
My argument in the above case is that the "friendly text" you are
echoing is wrong -- backwards.
min(-10000, -1000, -100, NULL, 100, 1000) returns NULL.
is not following standard PHP comparison rules.
It is. Never mind the 6 different vars, as the sort runs, it boils
down to
-10000 < NULL
(bool)-10000 < (bool)NULL
true < false
--> false
Hi!
Our manual does not have NULL/BOOL type behavior explanations.
It does. I already sent the link, here it is again:
http://www.php.net/manual/en/language.operators.comparison.php
The bug report might be confusing, but it's is not complaining -1 !=
false or like, but it's complainingmin()
is not following standard PHP
comparison rules.
This is incorrect, it does.
if (-1 < NULL)
echo 'NULL is smaller';
else
echo '-1 is smaller';returns '-1 is smaller' as all of us expects.
Surely, but since in your script the branches are switched, NULL
is
actually smaller. Your script just prints "-1 is smaller" when NULL
is
smaller. Exactly as described in the manual.
NULL
is evaluated as 0 in scalar contexts, but not withmin()
.NULL
wins
I'm not sure what "scalar contexts" are, but NULL
is converted to 0 as
integer, however comparisons do not always do integer conversions.
Please see the manual again.
negative values because of the compare_function() in zend_operators.c.
compare_function() treats NULL/BOOL type special way. (i.e Not like
expression in 'if' statement or like)
This is not correct, all these operations treat NULL
in the same way.
You are welcome to show an example where it is treated differently, but
I think it's be rather hard as they all use the same compare_function.
I think feasible solution for
min()
misbehavior is to use new function
that uses standard PHP comparison rule. I'll check PHP functions that
min()
already uses standard PHP comparison rules, by calling
compare_function.
uses compare_function() later. There may be other array functions use
it. I'm not sure if there are 'internal' functions that does not perform
comparison(or conversion) non standard way like compare_function().
compare_function is the standard way. That's what comparison operators
use, just look at the code.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi Stas,
On Sun, Nov 10, 2013 at 7:06 PM, Stas Malyshev smalyshev@sugarcrm.comwrote:
uses compare_function() later. There may be other array functions use
it. I'm not sure if there are 'internal' functions that does not perform
comparison(or conversion) non standard way like compare_function().compare_function is the standard way. That's what comparison operators
use, just look at the code.
Yes, but the result differs when comparison is done as expression. (e.g. if
(expression))
I haven't look into this issue carefully yet, but simple test codes revel
the
difference. I guess engine is converting values in expression according to
context,
but not in min()
. That's the reason why (expression) and min()
result
differs,
I suppose. I would say it's a bug of min()
calling compare_function() as it
is now.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Yes, but the result differs when comparison is done as expression. (e.g. if
(expression))
I can't find any discrepancy with how NULL
behaves (as others have
pointed out, your earlier example was echoing the wrong message).
However, it does seem that TRUE
behaves differently, based on the test
code below (see http://3v4l.org/7HGVI for full results):
function which_is_lower($a, $b)
{
if ( $a < $b )
{
return $a;
}
else
{
return $b;
}
}
function compare_comparisons($a, $b)
{
echo 'min() gives: ';
var_dump( min($a, $b) );
echo 'comparison with < gives: ';
var_dump( which_is_lower($a, $b) );
}
echo "\n", '-1 vs TRUE: ', "\n";
compare_comparisons(-1, TRUE);
Giving
-1 vs TRUE:
min()
gives: int(-1) comparison with < gives: bool(true)
--
Rowan Collins
[IMSoP]
( -1 < true ) // false, because the two compare equal and the comparison must return a bool
( min( -1,true ) ) // -1, because the two compare equal and -1 was passed first
( min( true,-1 ) ) // true, because the two compare equal and true was passed first
Absolutely consistent with documentation.
Pay attention to the final return type of a simple comparison vs.
comparisons wrapped in the min()
function.
-- S.
( -1 < true ) // false, because the two compare equal and the comparison must return a bool
( min( -1,true ) ) // -1, because the two compare equal and -1 was passed first
( min( true,-1 ) ) // true, because the two compare equal and true was passed first
Absolutely consistent with documentation.
Pay attention to the final return type of a simple comparison vs.
comparisons wrapped in themin()
function.-- S.
Thanks, I thought there was probably an obvious explanation.
Effectively, they're equal, so which of them is lower is "undefined";
min()
made one assumption (whichever came first), my dummy function with
< made the other (whichever came second, because it fell into the else{}).
--
Rowan Collins
[IMSoP]
min()
behavior is nonsense and it would be good fixingmin()
at some
point. Not many users will be affected, IMO.
Seriously, "O" isn't how one should decide to make such a fundamental
change to PHP...
Anyway, like Stas, I don't really understand the assumptions and
conclusions you draw in bug #53104.
You claim "Comparison operators evaluate comparison as PHP users
expect, but min()
does not. This behavior is not intuitive." (Though
you didn't cite any expect/intuit statistics.) Your example is:
( -1 > NULL
) // bool(true)
( -1 < NULL) // bool(false)
min(-1,NULL)) // NULL
Yet these examples are entirely consistent once you accept that the
comparison operator must (in PHP) return a boolean, while min()
can
have a dynamic return type and value. (Contrast this with SQL, in
which a comparison may return NULL. If we needed to defensively code
in PHP for simple operators to return NULL, we'd then need to make
constant use of COALESCE-type wrappers in PHP. This is a dangerous
slope.)
Not to mention that they are consistent with the docs.
To me, inconsistent would be:
( -1 > NULL
) // bool(false)
( -1 < NULL) // bool(true)
min(-1,NULL)) // NULL
If you had that, you'd have a smoking gun.
-- Sandy
Hi Sanford,
On Sun, Nov 10, 2013 at 5:55 PM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
To me, inconsistent would be:
( -1 >
NULL
) // bool(false)
( -1 < NULL) // bool(true)
min(-1,NULL)) //NULL
PHP is not SQL :)
I think almost all users are not expecting
min(-10000, -1000, -100, NULL, 100, 1000) returns NULL.
Besides, the manual is not explaining this weird behavior at all.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
I think almost all users are not expecting
min(-10000, -1000, -100, NULL, 100, 1000) returns NULL.
I expect it.
Because, in fact, PHP is not SQL.
It is PHP, and it has its own rules that everybody plays by. They
aren't SQL rules (where NULLs may either be disregarded, as in
aggregates, or quite heavily regarded).
The ecosystem of PHP and SQL is, however, one of the fundamental
reasons you must not make the change you propose. SQL MIN()
works
across one column within a rowset, while users often desire a similar
function that works across columns in a row. As they are unable to do
this in pure SQL, they will turn to application code, performing a PHP
min()
across columns returned from SQL -- columns that quite commonly
contain PHP NULL if the DB layer supports it. All this time, they have
been getting NULL. You want them to get -10000 for those same rows and
not have that affect their application in a big way. Just... no.
Besides, the manual is not explaining this weird behavior at all.
Sure it is.
-- S.
Hi Sanford,
On Sun, Nov 10, 2013 at 6:20 PM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
Besides, the manual is not explaining this weird behavior at all.
Sure it is.
Is it?
I should be missing some manual pages.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Is it?
I should be missing some manual pages.
The values are referred to as comparables, href'd to the comparison
page.
min()
follows the rules on the linked page, plus the additional note
about strings (which is not relevant to your objection).
Do you want the comparison rules to be restated on every page that
uses the rules? Sir Tim would not be proud.
-- S.
Hi Sanford,
On Sun, Nov 10, 2013 at 6:53 PM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
Is it?
I should be missing some manual pages.The values are referred to as comparables, href'd to the comparison
page.
min()
follows the rules on the linked page, plus the additional note
about strings (which is not relevant to your objection).
Are you referring user notes?
It's not a official documentation.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
The values are referred to as comparables, href'd to the comparison
page.
min()
follows the rules on the linked page, plus the additional note
about strings (which is not relevant to your objection).Are you referring user notes?
It's not a official documentation.
I didn't immediately spot it, but the word "comparable" next to each
argument in the Parameters section is linked to the comparison operators
page. It mgiht be an idea to add it to the See Also section as well, to
make it more visible.
--
Rowan Collins
[IMSoP]
I didn't immediately spot it, but the word "comparable" next to each
argument in the Parameters section is linked to the comparison operators
page. It mgiht be an idea to add it to the See Also section as well, to
make it more visible.
Don't see how this has any more of a visibility problem than mixed
type params for other functions.
IMO, it's a nice convenience that the hyperlink is there, but not even
necessary. min(<list>) should be assumed to be sort(<array>) and
unshift(<array>). Sort --> comparison --> comparison rules apply.
Do we really have to reiterate anywhere that a comparison might be
performed that it's going to follow comparison rules? If a fn accepts
a callback, do we say, "Remember how comparisons work"?
-- S.
Sanford Whiteman wrote (on 10/11/2013):
I didn't immediately spot it, but the word "comparable" next to each
argument in the Parameters section is linked to the comparison operators
page. It mgiht be an idea to add it to the See Also section as well, to
make it more visible.
Don't see how this has any more of a visibility problem thanmixed
type params for other functions.
Well, "mixed" is a pseudo-type used consistently throughout the manual
and linked to an explanation of the term here:
http://php.net/manual/en/language.pseudo-types.php
The fact that a particular value has to be "comparable", on the other
hand, doesn't really tell you how the different parameters interact, so
the link has poor "information scent" - I'd probably click there if my
values were being rejected, but not if certain combinations were
behaving "oddly". The reason I suggested a "See also" is that it's
unobtrusive, but can include clear text as to why you might want to look
at it.
Actually, now I look, most of the other text specifically talks only
about numeric (e.g. the "Return Values" section: "min() returns the
numerically lowest of the parameter values" which is just plain wrong)
or alphanumeric (in the "Note:" within the "Description" section)
values. This could easily reinforce the kind of mistake made here of
thinking that everything is cast to int (or possibly string) before
comparison.
IMO, it's a nice convenience that the hyperlink is there, but not even
necessary. min(<list>) should be assumed to be sort(<array>) and
unshift(<array>). Sort --> comparison --> comparison rules apply.
That's certainly an intuitive explanation, but it's by no means the only
way of implementing or imagining the underlying algorithm. It would be
one way of making clear what happens though: "This function is
equivalent to sorting the items based on the [link]standard type
comparisons[/link] and returning the first item."
You don't have to think the current documentation is awful to find ways
of improving it.
Regards,
Rowan Collins
[IMSoP]
You don't have to think the current documentation is awful to find ways
of improving it.
Sure, go for it!
-- S.
Hi all,
On Tue, Nov 12, 2013 at 4:03 AM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
You don't have to think the current documentation is awful to find ways
of improving it.Sure, go for it!
I wrote my misunderstanding to my personal blog. It seems many people
misunderstand it, just like me :)
Any improvement/correction?
min()
manual page is better to be updated. I'll add min()
example blow to
min()
manual
page also.
Index: operators.xml
--- operators.xml (リビジョン 332115)
+++ operators.xml (作業コピー)
@@ -1262,7 +1262,7 @@
<row>
<entry><type>bool</type> or <type>null</type></entry>
<entry>anything</entry>
-
<entry>Convert to <type>bool</type>, &false; < &true;</entry>
-
<entry>Convert to <type>bool</type> both side, &false; <
&true;</entry>
</row>
<row>
<entry><type>object</type></entry>
@@ -1298,8 +1298,28 @@
</tbody>
</tgroup>
</table>
- <para> <example>
-
<title>Boolean/null comparison</title>
-
<programlisting role="php">
+<![CDATA[ +<?php +// Bool and null are compared as bool always +var_dump(1 == TRUE); // `TRUE` - same as (bool)1 == `TRUE` +var_dump(0 == FALSE); // `TRUE` - same as (bool)0 == `FALSE` +var_dump(100 < TRUE); // `FALSE` - same as (bool)100 < `TRUE` +var_dump(-10 < FALSE);// `FALSE` - same as (bool)-10 < `FALSE` +var_dunp(min(-100,-10, NULL, 10, 100); // `NULL` - (bool)NULL < (bool)-100 is `FALSE` < `TRUE` +?> +]]>
-
</programlisting>
- </example>
- </para>
- <para>
- <example> <title>Transcription of standard array comparison</title> <programlisting role="php">
Hi,
What about the following PHP logic:
var_dump(null == 0); // TRUE
var_dump(null < -1); // TRUE
var_dump(null > -1); // FALSE
How can you explain that ?
Greetings
Marc
Am 15.11.2013 00:03, schrieb Yasuo Ohgaki:
Hi all,
On Tue, Nov 12, 2013 at 4:03 AM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:You don't have to think the current documentation is awful to find ways
of improving it.Sure, go for it!
I wrote my misunderstanding to my personal blog. It seems many people
misunderstand it, just like me :)Any improvement/correction?
min()
manual page is better to be updated. I'll addmin()
example blow to
min()
manual
page also.Index: operators.xml
--- operators.xml (リビジョン 332115)
+++ operators.xml (作業コピー)
@@ -1262,7 +1262,7 @@
<row>
<entry><type>bool</type> or <type>null</type></entry>
<entry>anything</entry>
<entry>Convert to <type>bool</type>, &false; < &true;</entry>
<entry>Convert to <type>bool</type> both side, &false; <
&true;</entry>
</row>
<row>
<entry><type>object</type></entry>
@@ -1298,8 +1298,28 @@
</tbody>
</tgroup>
</table>
- <para> <example>
<title>Boolean/null comparison</title>
<programlisting role="php">
+<![CDATA[ +<?php +// Bool and null are compared as bool always +var_dump(1 == TRUE); // `TRUE` - same as (bool)1 == `TRUE` +var_dump(0 == FALSE); // `TRUE` - same as (bool)0 == `FALSE` +var_dump(100 < TRUE); // `FALSE` - same as (bool)100 < `TRUE` +var_dump(-10 < FALSE);// `FALSE` - same as (bool)-10 < `FALSE` +var_dunp(min(-100,-10, NULL, 10, 100); // `NULL` - (bool)NULL < (bool)-100 is `FALSE` < `TRUE` +?> +]]>
<![CDATA[
</programlisting>
- </example>
- </para>
- <para>
- <example> <title>Transcription of standard array comparison</title> <programlisting role="php">
On Sat, Nov 16, 2013 at 6:22 AM, Marc Bennewitz php@marc-bennewitz.dewrote:
Hi,
What about the following PHP logic:
var_dump(null == 0); //TRUE
var_dump(null < -1); //TRUE
var_dump(null > -1); //FALSE
How can you explain that ?
Greetings
Marc
false ==false // TRUE
false < true // TRUE
false > true // FALSE
It's right here:
http://www.php.net/manual/en/language.operators.comparison.php. Halfway
down the page, type of op 1 is null, op 2 is anything.
--
William Bartlett
College of Engineering | Cornell University '14
240-432-5189
Am 15.11.2013 00:03, schrieb Yasuo Ohgaki:
Hi all,
On Tue, Nov 12, 2013 at 4:03 AM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:You don't have to think the current documentation is awful to find ways
of improving it.Sure, go for it!
I wrote my misunderstanding to my personal blog. It seems many people
misunderstand it, just like me :)Any improvement/correction?
min()
manual page is better to be updated. I'll addmin()
example blow to
min()
manual
page also.Index: operators.xml
--- operators.xml (リビジョン 332115)
+++ operators.xml (作業コピー)
@@ -1262,7 +1262,7 @@
<row>
<entry><type>bool</type> or <type>null</type></entry>
<entry>anything</entry>
<entry>Convert to <type>bool</type>, &false; < &true;</entry>
<entry>Convert to <type>bool</type> both side, &false; <
&true;</entry>
</row>
<row>
<entry><type>object</type></entry>
@@ -1298,8 +1298,28 @@
</tbody>
</tgroup>
</table>
- <para> <example>
<title>Boolean/null comparison</title>
<programlisting role="php">
+<![CDATA[ +<?php +// Bool and null are compared as bool always +var_dump(1 == TRUE); // `TRUE` - same as (bool)1 == `TRUE` +var_dump(0 == FALSE); // `TRUE` - same as (bool)0 == `FALSE` +var_dump(100 < TRUE); // `FALSE` - same as (bool)100 < `TRUE` +var_dump(-10 < FALSE);// `FALSE` - same as (bool)-10 < `FALSE` +var_dunp(min(-100,-10, NULL, 10, 100); // `NULL` - (bool)NULL < (bool)-100 is `FALSE` < `TRUE` +?> +]]>
<![CDATA[
</programlisting>
- </example>
- </para>
- <para>
- <example> <title>Transcription of standard array comparison</title> <programlisting role="php">
You don't have to think the current documentation is awful to find ways
of improving it.
Sure, go for it!-- S.
I've had a busy week, but have now submitted a patch via edit.php.net
(copy attached) which tidies up both min()
and max()
documentation to
refer to the standard comparisons rather than the misleading mentions of
"numeric comparison" etc.
I've also made them use the same examples wherever it makes sense, since
they really are exact counter-parts to one another.
Any feedback is of course welcome. :)
--
Rowan Collins
[IMSoP]
Hi all,
I was surprised that wired
min()
behavior.
The cause of this behavior is in Zend API.https://bugs.php.net/bug.php?id=53104
So I've made FRC to discuss/document/fix such behavior.
If anyone know such behavior, please let me know so that I can write them
in the RFC.Thank you.
Apparently, I misunderstood the expression evaluation of NULL/FALSE of PHP
for my
entire PHP life :(
The reason why I misunderstood the behavior is
[yohgaki@dev ~]$ php -r "var_dump((TRUE < 1));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE > 1));"
bool(false)
TRUE
always evaluated as 1. I guess I've checked this behavior and made
wrong assumption
FALSE/NULL is always evaluated as 0, but it's not.
[yohgaki@dev ~]$ php -r "var_dump((FALSE == 0));"
bool(true)
[yohgaki@dev ~]$ php -r "var_dump((FALSE < 0));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((FALSE > 0));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((NULL == 0));"
bool(true)
[yohgaki@dev ~]$ php -r "var_dump((NULL < 0));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((NULL > 0));"
bool(false)
FALSE/NULL are evaluated as 0 or less(least).
Then the manual could be improved.
Comparison with Various Types Type of Operand 1Type of Operand 2Result
null http://www.php.net/manual/en/language.types.null.php or
stringhttp://www.php.net/manual/en/language.types.string.php
string http://www.php.net/manual/en/language.types.string.php Convert
NULL to "", numerical or lexical comparison
boolhttp://www.php.net/manual/en/language.types.boolean.php
or null http://www.php.net/manual/en/language.types.null.php anythingConvert
to bool http://www.php.net/manual/en/language.types.boolean.php, FALSE
< TRUE object http://www.php.net/manual/en/language.types.object.php
object http://www.php.net/manual/en/language.types.object.php Built-in
classes can define its own comparison, different classes are uncomparable,
same class - compare properties the same way as arrays (PHP 4), PHP 5 has
its own explanationhttp://www.php.net/manual/en/language.oop5.object-comparison.php
string http://www.php.net/manual/en/language.types.string.php,resourcehttp://www.php.net/manual/en/language.types.resource.php
ornumberhttp://www.php.net/manual/en/language.pseudo-types.php#language.types.number
string http://www.php.net/manual/en/language.types.string.php,resourcehttp://www.php.net/manual/en/language.types.resource.php
ornumberhttp://www.php.net/manual/en/language.pseudo-types.php#language.types.number
Translate
strings and resources to numbers, usual math
arrayhttp://www.php.net/manual/en/language.types.array.php
array http://www.php.net/manual/en/language.types.array.php Array with
fewer members is smaller, if key from operand 1 is not found in operand 2
then arrays are uncomparable, otherwise - compare value by value (see
following example)
objecthttp://www.php.net/manual/en/language.types.object.phpanything
object http://www.php.net/manual/en/language.types.object.php is always
greater array http://www.php.net/manual/en/language.types.array.phpanything
array http://www.php.net/manual/en/language.types.array.php is always
greater
http://php.net/manual/en/language.operators.comparison.php
Manual states
bool or null anything Convert to bool, FALSE
< TRUE
[yohgaki@dev ~]$ php -r "var_dump((bool)NULL);"
bool(false)
I really surprised by myself that I've never bitten by the misunderstanding
more than 10 years.
Anyway, the manual is better to explain explicitly
TRUE
always evaluated as 1 while
NULL/FALSE is evaluated as 0 or least.
Sorry for the confusion.
Any comments?
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Anyway, the manual is better to explain explicitly
TRUE
always evaluated as 1 while
NULL/FALSE is evaluated as 0 or least.
No, FALSE
< TRUE
is still the most appropriate way to express this.
"Explaining" a strict boolean comparison in terms of integers/bits is
misleading.
-- S.
Hi all,
I really surprised by myself that I've never bitten by the
misunderstanding more than 10 years.
Anyway, the manual is better to explain explicitly
TRUE
always evaluated as 1 while
NULL/FALSE is evaluated as 0 or least.Sorry for the confusion.
Any comments?
I still didn't get it right :(
[yohgaki@dev ~]$ php -r "var_dump((TRUE > 100));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE < 100));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE > -100));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE < -100));"
bool(false)
TRUE/FALSE only evaluated as 1/0 otherwise always return FALSE
in
expression.
[yohgaki@dev ~]$ php -r "var_dump((NULL < -100));"
bool(true)
[yohgaki@dev ~]$ php -r "var_dump((NULL > -100));"
bool(false)
NULL
behaves differently, though.
Because min()
/max() has to use < or > results became depends on how it
compares.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
I still didn't get it right :(
Please, please read the documentation for comparison, juggling, and
precedence.
[yohgaki@dev ~]$ php -r "var_dump((TRUE > 100));"
bool(false)
Equiv to TRUE
> TRUE
[yohgaki@dev ~]$ php -r "var_dump((TRUE < 100));"
bool(false)
Equiv to TRUE
< TRUE
[yohgaki@dev ~]$ php -r "var_dump((TRUE > -100));"
bool(false)
Equiv to TRUE
> TRUE
[yohgaki@dev ~]$ php -r "var_dump((TRUE < -100));"
bool(false)
Equiv to TRUE
< TRUE
TRUE/FALSE only evaluated as 1/0 otherwise always return
FALSE
in
expression.
You must stop phrasing it that way -- that may be accidentally correct
for some situations but it is not "the rule."
[yohgaki@dev ~]$ php -r "var_dump((NULL < -100));"
bool(true)
Equiv to FALSE
< TRUE
[yohgaki@dev ~]$ php -r "var_dump((NULL > -100));"
bool(false)
Equiv to FALSE
> TRUE
NULL
behaves differently, though.
Differently from what? It's exactly as specified in the docs.
-- S.
Hi Sanford,
On Mon, Nov 11, 2013 at 5:28 AM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
[yohgaki@dev ~]$ php -r "var_dump((NULL < -100));"
bool(true)Equiv to
FALSE
<TRUE
[yohgaki@dev ~]$ php -r "var_dump((NULL > -100));"
bool(false)Equiv to
FALSE
>TRUE
NULL
behaves differently, though.Differently from what? It's exactly as specified in the docs.
My previous post is wrong.
TRUE
only evaluated as 1 otherwise FALSE.
FALSE/NULL evaluated as 0 or least.
That's what we need in document. IMO.
However, this behavior seems weird.
[yohgaki@dev ~]$ php -r "var_dump((FALSE < -100));"
bool(true)
[yohgaki@dev ~]$ php -r "var_dump((FALSE > -100));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE < -100));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE > -100));"
bool(false)
It's really confusing to me :(
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
TRUE
only evaluated as 1 otherwise FALSE. FALSE/NULL evaluated as 0
or least.
This is not helping you understand -- you're trying to create rules
and fitting some behavior into them, and then withdrawing your "rules"
when they appear to break.
What you should be doing is using the actual stated rules of the
language.
I realize you're excited about correcting something that has confused
you for a long time, but you are getting ahead of yourself in trying
to say there's something fundamentally wrong with the way PHP works.
Try to slow down and read the comparison rules.
That's what we need in document. IMO.
We absolutely, positively do not need something like this in the
docs.
However, this behavior seems weird.
[yohgaki@dev ~]$ php -r "var_dump((FALSE < -100));"
bool(true)
[yohgaki@dev ~]$ php -r "var_dump((FALSE > -100));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE < -100));"
bool(false)
[yohgaki@dev ~]$ php -r "var_dump((TRUE > -100));"
bool(false)
It's really confusing to me :(
OK, first throw out EVERYTHING you have assumed to date.
Now, realize that PHP has a documented rule of comparing booleans to
integers (this rule is found in the "NULL or boolean - to - anything"
part of the page on comparison).
The rule is: compare as booleans, where FALSE
< TRUE.
So take
FALSE
< -100
How do we compare them as booleans?
• `FALSE` is already a bool: use `FALSE`
• cast -100 to bool: use `TRUE`
So the effective strict comparison is
FALSE
< TRUE
By the definition of boolean comparison in PHP, this expression must
be TRUE.
How about
`TRUE` < -100
Compare as booleans?
• `TRUE` is a boolean: use `TRUE`
• cast -100 to bool: use `TRUE`
So we've got
TRUE
< TRUE
By the definition of non-object comparison in most any language
(including PHP), this expression is FALSE.
-- S.
On Mon, Nov 11, 2013 at 6:11 AM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
OK, first throw out EVERYTHING you have assumed to date.
Now, realize that PHP has a documented rule of comparing booleans to
integers (this rule is found in the "NULL or boolean - to - anything"
part of the page on comparison).The rule is: compare as booleans, where
FALSE
< TRUE.So take
FALSE
< -100How do we compare them as booleans?
• `FALSE` is already a bool: use `FALSE` • cast -100 to bool: use `TRUE`
So the effective strict comparison is
FALSE
<TRUE
By the definition of boolean comparison in PHP, this expression must
be TRUE.How about
`TRUE` < -100
Compare as booleans?
• `TRUE` is a boolean: use `TRUE` • cast -100 to bool: use `TRUE`
So we've got
TRUE
<TRUE
By the definition of non-object comparison in most any language
(including PHP), this expression is FALSE.
Root cause of my misunderstanding is wrong assumption that BOOL/NULL are
evaluated as INT, but it's BOOL.
Thank you for your time.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Root cause of my misunderstanding is wrong assumption that
BOOL/NULL are evaluated as INT, but it's BOOL.
Alright!
Hi!
TRUE
always evaluated as 1. I guess I've checked this behavior and made
wrong assumption
FALSE/NULL is always evaluated as 0, but it's not.
True is not evaluated as 1, 1 is evaluated as true. When you compare
with bool, other side is converted to bool. Which is described in the
manual :) Try the same with -17, you'd get the same result, since -17 is
bool-eanized as true.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi Stas,
On Mon, Nov 11, 2013 at 6:17 AM, Stas Malyshev smalyshev@sugarcrm.comwrote:
TRUE
always evaluated as 1. I guess I've checked this behavior and made
wrong assumption
FALSE/NULL is always evaluated as 0, but it's not.True is not evaluated as 1, 1 is evaluated as true. When you compare
with bool, other side is converted to bool. Which is described in the
manual :) Try the same with -17, you'd get the same result, since -17 is
bool-eanized as true.
bool or null anything Convert to bool, FALSE
< TRUE
I was reading null gets converted to bool, but it's on both ops.
It's surprising to me that I've never bitten by this misunderstanding :(
Thank you for your time.
--
Yasuo Ohgaki
yohgaki@ohgaki.net