Jump to content


Check out our Community Blogs





- - - - -

Illogical PHP Logic

Posted by John, 17 July 2009 · 689 views

In algebra, after we learn the basic distributive, commutative, and associative properties, the transitive property of equality is usually next in the curriculum. For those of you who do not recall the terminology, the transitive property of equality says if $a == $b and $b == $n then $a == $n. Using this age old logic, you can prove FALSE == TRUE and 0 == 1 in PHP. Here is how:


$a = 0;
$b = "Hooray for PHP logic and dynamic type casting?";
var_dump(((FALSE == $a) == ($a == $b)) == ($b == TRUE));
var_dump((((0 == $a) == ($a == $b)) == ($b == TRUE)) == (TRUE == 1));

Returns
bool(true)
bool(true)
Q.E.D

Note: I am comparing values not types. That being said, forgive me when I flame you for using PHP inappropriately. A string is NOT an integer and an integer is NOT a boolean. They should not be used as such.

Originally posted on johnciacia.com

  • 0



Yup. That's pretty screwed up.
    • 0
I refuse to answer (this is not an answer :) ) until I've had enough time to evaluate what you've stated here.
    • 0
Totally!!
    • 0

I refuse to answer (this is not an answer :) ) until I've had enough time to evaluate what you've stated here.

What are you going to answer? I didn't ask any questions.
    • 0
Replace answer with reply, genius.
    • 0
I've noticed a lot of your replies are smart-** replies, such as the purple one above. I don't believe you are so incredibly stupid that you couldn't figure out what he was saying, or are you?

As for this post, are you making fun of PHP or not? I can't tell.

The first var_dump states (false) == (false) == (false) so yes, false does = false which is true. I'm not sure wtf you are bitching about?
    • 0
Agreed with Winged. This is very screwed up!
    • 0

I've noticed a lot of your replies are smart-** replies, such as the purple one above. I don't believe you are so incredibly stupid that you couldn't figure out what he was saying, or are you?

As for this post, are you making fun of PHP or not? I can't tell.

The first var_dump states (false) == (false) == (false) so yes, false does = false which is true. I'm not sure wtf you are bitching about?


WTF are you talking about? HTF do you get (false) == (false) == (false)?

var_dump(FALSE == 0);  //true
var_dump(0 == "a string"); //true
var_dump("a string" == TRUE); //true

I am getting (true) == (true) == (true) which is indeed true. I am simply questioning how "a string" has the value TRUE if "a string" also has the value 0 which itself has the value FALSE? To answer your question, yes I am making fun of PHP.
    • 0
Hey, I don't know PHP but how do you figure, ****smack?

0 != boolean false
0 != "Hooray for PHP logic and dynamic type casting?"
"Hooray for PHP logic and dynamic type casting?"; != boolean false

So what are you on about? Are you on drugs?
    • 0
No **** you ******* idiot. That is the whole ******* point. 0 != boolean false yet PHP returns 0 == boolean false. God **** your a ******* genius.
    • 0
@Relapse: PHP is a loosely typed language which means there is type juggling for variables depending on the context they are used in. See the PHP Manual page for more information and why John's error occurs: http://us3.php.net/m...pe-juggling.php

Using a triple equal in the condition above would compare type and value, resulting in what you stated.
    • 0

@Relapse: PHP is a loosely typed language which means there is type juggling for variables depending on the context they are used in. See the PHP Manual page for more information and why John's error occurs: http://us3.php.net/m...pe-juggling.php

Using a triple equal in the condition above would compare type and value, resulting in what you stated.

I made no error, I am comparing the values not the types - i noted that in my note. I am pointing this out for everyone who does not know about the triple equals.
    • 0
You didn't make the error, PHP did/does. Of course, you are going to say "That isn't an error, that is how it works" but that would be contradictory to your original post.

"A string is NOT an integer and an integer is NOT a boolean. They should not be used as such."
    • 0

No **** you ******* idiot. That is the whole ******* point. 0 != boolean false yet PHP returns 0 == boolean false. God **** your a ******* genius.


Wow, you are an awesome flamer. If you know why the language made these errors, why are you bitching about it? You are the type of person that ** to **? A Complainer?
    • 0
I've deleted some of the comments here because they were utterly useless and insulting.
    • 0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download