View RSS Feed

John

Illogical PHP Logic

Rate this Entry
by , 07-17-2009 at 03:53 PM (1363 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:

Code:
$a 0;
$b "Hooray for PHP logic and dynamic type casting?";
var_dump(((FALSE == $a) == ($a == $b)) == ($b == TRUE));
var_dump((((== $a) == ($a == $b)) == ($b == TRUE)) == (TRUE == 1)); 
Returns
Code:
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

Submit "Illogical PHP Logic" to Digg Submit "Illogical PHP Logic" to del.icio.us Submit "Illogical PHP Logic" to StumbleUpon Submit "Illogical PHP Logic" to Google

Updated 07-17-2009 at 08:37 PM by John

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. WingedPanther's Avatar
    Yup. That's pretty screwed up.
  2. Jordan's Avatar
    I refuse to answer (this is not an answer ) until I've had enough time to evaluate what you've stated here.
  3. dvelop's Avatar
    Totally!!
  4. John's Avatar
    Quote Originally Posted by Jordan
    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.
  5. Jordan's Avatar
    Replace answer with reply, genius.
  6. Brandon W's Avatar
    Agreed with Winged. This is very screwed up!
  7. Jordan's Avatar
    @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/manual/en/languag...e-juggling.php

    Using a triple equal in the condition above would compare type and value, resulting in what you stated.
  8. John's Avatar
    Quote Originally Posted by Jordan
    @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/manual/en/languag...e-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.
  9. Jordan's Avatar
    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."
  10. Jordan's Avatar
    I've deleted some of the comments here because they were utterly useless and insulting.