php-windows Digest 17 Oct 2005 14:42:20 -0000 Issue 2798
Topics (messages 26413 through 26416):
Re: Is this a PHP bug?
26413 by: Paul Menard
26414 by: Nadim Attari
26415 by: Ross Honniball
26416 by: tg-php.gryffyndevelopment.com
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
Well per this PHP page:
http://us2.php.net/manual/en/language.operators.comparison.php,
"If you compare an integer with a string, the string is converted to a number. "
So to follow this regarding your example:
$x = 0; // Numeric zero
$y = 'Some kind of string';
if (is_null($x)) echo "x is NULL<br>";
if (is_null($y)) echo "y is NULL<br>";
echo "x is [" . gettype($x) . "]<br>";
echo "y is [" . gettype($y) . "]<br>";
echo "y as integer=[" . (int)($y) . "]<br>";
if ($x == $y) echo 'they equal using ==';
if ($x === $y) echo 'they equal using ===';
As you might see from the line 'echo "y as integer...' if you cast the string,
$y, as an integer
per the PHP page referenced above the value reported is integer zero. So this
is how if can equal
$x which is already an integer and value zero itself.
No for some of you assumptions philosophies etc.
Many think PHP is a typeless language. This is not accurate. PHP is actually a
loosley-typed
language. This is evident from the ability to assign type to variables. Also in
the fact that you
are allowed to compared variables to each other (at you own risk) even if the
types do not match.
This is very unlike other strong-typed languages like C and C++.
The designers of the PHP language semantics had to make a choice in how a
compare of mixed-typed
variables would be handled. In my opinion, being a C++ developer in a former
life, it is easier to
convert the string to numeric than the numeric to string for the 'on-the-fly'
compare.
I hope this helps your understanding. My suggestion is if you are still of the
mind this is a bug
report it to PHP.
P-
--- Ross Honniball <[EMAIL PROTECTED]> wrote:
> I'm yet to be convinced it isn't a bug. I suspect it is a bug that has been
> around so long that
> they can't afford to fix it or it may break many applications that for
> various peculiar reasons
> rely on it to behave as it does for their code to work.
>
> For those who have tried to defend its behaviour, this is my logic.
>
> 1. The == operator means, by definition, "don't worry about variable 'type',
> just compare the
> values"
> (This is probably the single thing I love about PHP more than anything
> else)
>
> 2. If the numeric variable $x is given ANY other numeric value other than
> zero,
> the logic behaves correctly as you read it
>
> So it is irrational for program logic to vary between a zero value and, for
> aguments sake, a
> numeric value of 42.
>
> Regarding specifically TG's explanation below, I believe he is wrong. If you
> compare a numeric
> type and a string type using ==, PHP should convert the number to a string
> and compare the two
> from there.
>
> Anyway, not expecting any answers to this, just a point of note and a strange
> quirk to keep at
> the back of your head when consciously comparing numeric / string variables.
>
> Ross
>
> [EMAIL PROTECTED] wrote:
> > It's because PHP is trying to convert different var types into a common var
> > type and then
> compare them when you use ==. === means compare value AND type so that's why
> it fails.
> >
> > Your string, 'Some kind of string' is getting converted down to the lowest
> > common denominator,
> an int. Since there are no numbers in the string to grab onto, it gets
> converted to nothing, an
> int value of zero.
> >
> > If you had $x = "0" or if $y had contained numbers at all, it wouldn't have
> > passed.
> >
> > But this is why when you use $x.'' it works properly because now you have
> > "0<empty string>",
> it's been converted to a string value "0". Good catch on that though, shows
> good methodical
> debugging :)
> >
> > So in the future, either use === or be extra aware of your variable types
> > and make sure you're
> comparing properly.
> >
> > Good luck!
> >
> > -TG
> >
> > = = = Original message = = =
> >
> > $x = 0; // Numeric zero
> > $y = 'Some kind of string';
> >
> > if ($x == $y) echo 'they equal using ==';
> > if ($x === $y) echo 'they equal using ===';
> >
> > The above will echo 'they equal using =='.
> >
> > The values don't look very equal to me.
> >
> > Can anyone explain the logic behind this?
> >
> > I'm heading home now but look forward to your explanations tomorrow.
> >
> > PS
> >
> > Incidently, to 'fix' it so it behaves as it should, you can code:
> >
> > if ($x.'' == $y.'') echo 'this will not print and all is good.';
> >
> > Regards .. Ross
> >
> >
> > ___________________________________________________________
> > Sent by ePrompter, the premier email notification software.
> > Free download at http://www.ePrompter.com.
> >
> >
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
Ross Honniball wrote:
I'm yet to be convinced it isn't a bug. I suspect it is a bug that has
been around so long that they can't afford to fix it or it may break
many applications that for various peculiar reasons rely on it to
behave as it does for their code to work.
Read this first
http://www.blueshoes.org/en/developer/php_cheat_sheet/
Then do a test here:
http://www.blueshoes.org/en/developer/syntax_exam/
Hope this will *fix* your bug !
Best Regards,
Nadim Attari
Alienworkers.com
For those who have tried to defend its behaviour, this is my logic.
1. The == operator means, by definition, "don't worry about variable
'type', just compare the values"
(This is probably the single thing I love about PHP more than
anything else)
2. If the numeric variable $x is given ANY other numeric value other
than zero, the logic behaves correctly as you read it
So it is irrational for program logic to vary between a zero value
and, for aguments sake, a numeric value of 42.
Regarding specifically TG's explanation below, I believe he is wrong.
If you compare a numeric type and a string type using ==, PHP should
convert the number to a string and compare the two from there.
Anyway, not expecting any answers to this, just a point of note and a
strange quirk to keep at the back of your head when consciously
comparing numeric / string variables.
Ross
[EMAIL PROTECTED] wrote:
It's because PHP is trying to convert different var types into a
common var type and then compare them when you use ==. === means
compare value AND type so that's why it fails.
Your string, 'Some kind of string' is getting converted down to the
lowest common denominator, an int. Since there are no numbers in the
string to grab onto, it gets converted to nothing, an int value of zero.
If you had $x = "0" or if $y had contained numbers at all, it
wouldn't have passed.
But this is why when you use $x.'' it works properly because now you
have "0<empty string>", it's been converted to a string value "0".
Good catch on that though, shows good methodical debugging :)
So in the future, either use === or be extra aware of your variable
types and make sure you're comparing properly.
Good luck!
-TG
= = = Original message = = =
$x = 0; // Numeric zero
$y = 'Some kind of string';
if ($x == $y) echo 'they equal using ==';
if ($x === $y) echo 'they equal using ===';
The above will echo 'they equal using =='.
The values don't look very equal to me.
Can anyone explain the logic behind this?
I'm heading home now but look forward to your explanations tomorrow.
PS
Incidently, to 'fix' it so it behaves as it should, you can code:
if ($x.'' == $y.'') echo 'this will not print and all is good.';
Regards .. Ross
--- End Message ---
--- Begin Message ---
I had a look at the link and couldn't find the quote you have below, but I am
happy to believe you.
Viewed in this context, I guess it does behave correctly and predictably.
I'll have to be more careful with my type mixing in future.
Thanks for your input ... Ross
Paul Menard wrote:
Well per this PHP page:
http://us2.php.net/manual/en/language.operators.comparison.php,
"If you compare an integer with a string, the string is converted to a number. "
So to follow this regarding your example:
$x = 0; // Numeric zero
$y = 'Some kind of string';
if (is_null($x)) echo "x is NULL<br>";
if (is_null($y)) echo "y is NULL<br>";
echo "x is [" . gettype($x) . "]<br>";
echo "y is [" . gettype($y) . "]<br>";
echo "y as integer=[" . (int)($y) . "]<br>";
if ($x == $y) echo 'they equal using ==';
if ($x === $y) echo 'they equal using ===';
As you might see from the line 'echo "y as integer...' if you cast the string,
$y, as an integer
per the PHP page referenced above the value reported is integer zero. So this
is how if can equal
$x which is already an integer and value zero itself.
No for some of you assumptions philosophies etc.
Many think PHP is a typeless language. This is not accurate. PHP is actually a
loosley-typed
language. This is evident from the ability to assign type to variables. Also in
the fact that you
are allowed to compared variables to each other (at you own risk) even if the
types do not match.
This is very unlike other strong-typed languages like C and C++.
The designers of the PHP language semantics had to make a choice in how a
compare of mixed-typed
variables would be handled. In my opinion, being a C++ developer in a former
life, it is easier to
convert the string to numeric than the numeric to string for the 'on-the-fly'
compare.
I hope this helps your understanding. My suggestion is if you are still of the
mind this is a bug
report it to PHP.
P-
--- Ross Honniball <[EMAIL PROTECTED]> wrote:
I'm yet to be convinced it isn't a bug. I suspect it is a bug that has been
around so long that
they can't afford to fix it or it may break many applications that for various
peculiar reasons
rely on it to behave as it does for their code to work.
For those who have tried to defend its behaviour, this is my logic.
1. The == operator means, by definition, "don't worry about variable 'type',
just compare the
values"
(This is probably the single thing I love about PHP more than anything else)
2. If the numeric variable $x is given ANY other numeric value other than zero,
the logic behaves correctly as you read it
So it is irrational for program logic to vary between a zero value and, for
aguments sake, a
numeric value of 42.
Regarding specifically TG's explanation below, I believe he is wrong. If you
compare a numeric
type and a string type using ==, PHP should convert the number to a string and
compare the two
from there.
Anyway, not expecting any answers to this, just a point of note and a strange
quirk to keep at
the back of your head when consciously comparing numeric / string variables.
Ross
[EMAIL PROTECTED] wrote:
It's because PHP is trying to convert different var types into a common var
type and then
compare them when you use ==. === means compare value AND type so that's why
it fails.
Your string, 'Some kind of string' is getting converted down to the lowest
common denominator,
an int. Since there are no numbers in the string to grab onto, it gets
converted to nothing, an
int value of zero.
If you had $x = "0" or if $y had contained numbers at all, it wouldn't have
passed.
But this is why when you use $x.'' it works properly because now you have "0<empty
string>",
it's been converted to a string value "0". Good catch on that though, shows
good methodical
debugging :)
So in the future, either use === or be extra aware of your variable types and
make sure you're
comparing properly.
Good luck!
-TG
= = = Original message = = =
$x = 0; // Numeric zero
$y = 'Some kind of string';
if ($x == $y) echo 'they equal using ==';
if ($x === $y) echo 'they equal using ===';
The above will echo 'they equal using =='.
The values don't look very equal to me.
Can anyone explain the logic behind this?
I'm heading home now but look forward to your explanations tomorrow.
PS
Incidently, to 'fix' it so it behaves as it should, you can code:
if ($x.'' == $y.'') echo 'this will not print and all is good.';
Regards .. Ross
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
Thanks for the vote of confidence, Ross. hah. *shrug*
At any rate, I was wrong about one thing. Mis-remembered something about
converting a string to a int. It doesn't pull all numbers out of the string,
it only pulls leading numbers.
$address = "123 Main St Apt 45";
echo intval($address);
outputs "123" not "12345"
But as explained, the lack of numbers (specifically at the front end of the
string) produces a numerical zero.
As to "numbers should be converted to strings" that was sort of my initial
thoughts when I started using PHP, but now that I've used it for a while, I
think the "convert to lowest common denominator" method works better. Either
way, it was a judgement call on the part of the PHP development team and it's
how PHP operates.
So at this point it's kind of like saying "This house really should have a
basement" after the house is built. Maybe there's a good reason why they
didn't put a basement in to start with and technically it's possible to add a
basement if they changed their mind, but at what cost.
At any rate, known, predictable and intentional behavior is not a bug.
-TG
= = = Original message = = =
I'm yet to be convinced it isn't a bug. I suspect it is a bug that has been
around so long that they can't afford to fix it or it may break many
applications that for various peculiar reasons rely on it to behave as it does
for their code to work.
For those who have tried to defend its behaviour, this is my logic.
1. The == operator means, by definition, "don't worry about variable 'type',
just compare the values"
(This is probably the single thing I love about PHP more than anything else)
2. If the numeric variable $x is given ANY other numeric value other than zero,
the logic behaves correctly as you read it
So it is irrational for program logic to vary between a zero value and, for
aguments sake, a numeric value of 42.
Regarding specifically TG's explanation below, I believe he is wrong. If you
compare a numeric type and a string type using ==, PHP should convert the
number to a string and compare the two from there.
Anyway, not expecting any answers to this, just a point of note and a strange
quirk to keep at the back of your head when consciously comparing numeric /
string variables.
Ross
[EMAIL PROTECTED] wrote:
> It's because PHP is trying to convert different var types into a common var
> type and then compare them when you use ==. === means compare value AND type
> so that's why it fails.
>
> Your string, 'Some kind of string' is getting converted down to the lowest
> common denominator, an int. Since there are no numbers in the string to grab
> onto, it gets converted to nothing, an int value of zero.
>
> If you had $x = "0" or if $y had contained numbers at all, it wouldn't have
> passed.
>
> But this is why when you use $x.'' it works properly because now you have
> "0<empty string>", it's been converted to a string value "0". Good catch on
> that though, shows good methodical debugging :)
>
> So in the future, either use === or be extra aware of your variable types and
> make sure you're comparing properly.
>
> Good luck!
>
> -TG
>
> = = = Original message = = =
>
> $x = 0; // Numeric zero
> $y = 'Some kind of string';
>
> if ($x == $y) echo 'they equal using ==';
> if ($x === $y) echo 'they equal using ===';
>
> The above will echo 'they equal using =='.
>
> The values don't look very equal to me.
>
> Can anyone explain the logic behind this?
>
> I'm heading home now but look forward to your explanations tomorrow.
>
> PS
>
> Incidently, to 'fix' it so it behaves as it should, you can code:
>
> if ($x.'' == $y.'') echo 'this will not print and all is good.';
>
> Regards .. Ross
>
>
> ___________________________________________________________
> Sent by ePrompter, the premier email notification software.
> Free download at http://www.ePrompter.com.
>
>
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.
--- End Message ---