Ping ([info]zestyping) wrote,
@ 2005-06-09 10:11:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Why PHP should never be taught.

Suppose A equals B, and also B equals C. Any reasonable person would expect that A equals C, right?

Oh yeah?

% cat equality.php
<?php

$a = 0;
$b = "eggs";
$c = "spam";

print ($a == $b) ? "a == b\n" : "a != b\n";
print ($b == $c) ? "b == c\n" : "b != c\n";
print ($a == $c) ? "a == c\n" : "a != c\n";
print ($a == $d) ? "a == d\n" : "a != d\n";
print ($b == $d) ? "b == d\n" : "b != d\n";
print ($c == $d) ? "c == d\n" : "c != d\n";

?>

% php equality.php
a == b
b != c
a == c
a == d
b != d
c != d

%
Try explaining that to a first-time programmer.

Update (2007-12-11): It gets even worse. Check this out.



Page 1 of 2
<<[1] [2] >>

(Post a new comment)


[info]nibot
2005-06-09 05:32 pm UTC (link)
Why PHP shouldn't be taught? This is why PHP shouldn't exist.

(Reply to this)(Thread)(Expand)

but, as a practical matter - [info]josephgrossberg, 2007-12-12 07:57 pm UTC (Expand)
Re: but, as a practical matter - [info]boffo9, 2007-12-12 08:48 pm UTC (Expand)
Added to My Memories
r6
2005-06-09 06:46 pm UTC (link)

This is even worse than being referentially opaque.

(Reply to this)


[info]rebbyribs
2005-06-09 07:02 pm UTC (link)
So 0 has a special meaning that wouldn't be intuitively obvious?

I guess I don't understand how that's so much worse than the rest of the symbols being not intuitively obvious ($ ; != = vs ==). It's pretty much all confusing when you're a first-time programmer, and it just seems like a bunch of picky arbitrary rules to remember.

(Reply to this)(Thread)(Expand)

(no subject) - (Anonymous), 2007-12-11 11:02 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-11 11:09 pm UTC (Expand)
(no subject) - [info]vityok, 2007-12-12 06:55 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 09:55 am UTC (Expand)

[info]duaiwe
2005-06-09 07:07 pm UTC (link)
I fail to see how PHP's overloading equality operators makes it necessarily a bad language. The meaning of the equality operators is entirely dependent on context. Not unlike overloading operators in C++.

A poor language to start first time programmers on? Very likely, I'd start with something like C that's a lot more strict. But that one example doesn't condemn the entire language.

(Reply to this)(Thread)(Expand)

(no subject) - [info]lexyeevee, 2007-12-11 09:54 pm UTC (Expand)
(no subject) - [info]jib.myopenid.com, 2007-12-12 03:48 am UTC (Expand)
(no subject) - [info]lexyeevee, 2007-12-12 04:16 pm UTC (Expand)

[info]trinity_gal
2005-06-09 07:12 pm UTC (link)
b and c don't even look like integers...
seems like any string equals zero
how does php define equality??

is there any usefulness for php defining equality like this?

(Reply to this)(Thread)(Expand)

(no subject) - [info]zestyping, 2005-06-09 08:28 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-11 10:46 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 02:24 am UTC (Expand)
(no subject) - [info]lexyeevee, 2007-12-12 04:58 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 02:46 am UTC (Expand)

[info]_rowan_tree_
2005-06-09 09:04 pm UTC (link)
That's why languages should have explicit types!

(Reply to this)(Thread)(Expand)

(no subject) - [info]misuba, 2005-06-09 09:45 pm UTC (Expand)
(no subject) - [info]eigenvalue, 2005-06-10 03:12 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 03:07 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 05:38 am UTC (Expand)
(no subject) - [info]praetorian42, 2007-12-12 01:21 pm UTC (Expand)
(no subject) - [info]_rowan_tree_, 2007-12-12 03:16 pm UTC (Expand)

[info]jerub
2005-06-10 05:01 am UTC (link)
Also, the boolean type 'True' is a bastard of a thing in php. Watch this:
<?php
function myFunc($foo) {
if ($foo == 'abc') {
echo "AbcCase\n";
}
if ($foo == 'bar') {
echo "FooCase\n";
}
}
myFunc(True);
?>
AbcCase
FooCase

(Reply to this)(Thread)(Expand)

(no subject) - (Anonymous), 2005-06-10 07:39 am UTC (Expand)
(no subject) - [info]jerub, 2005-06-10 08:59 am UTC (Expand)
(no subject) - [info]misuba, 2005-06-10 06:52 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-11 11:11 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 01:09 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 05:41 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 03:56 pm UTC (Expand)
Python
r6
2005-06-13 02:05 pm UTC (link)
$ python -V
Python 2.4.1
$ python
>>> a = 10000000000000001
>>> b = 10000000000000000.0+0.0j
>>> c = 9999999999999999
>>> a==b
True
>>> b==c
True
>>> a==c
False

Is this any different?

(Reply to this)(Thread)(Expand)

Re: Python - [info]zestyping, 2005-06-13 04:59 pm UTC (Expand)
Re: Python - r6, 2005-06-13 07:29 pm UTC (Expand)
Re: Python - [info]zestyping, 2005-06-13 08:07 pm UTC (Expand)
Re: Python - r6, 2005-06-13 08:46 pm UTC (Expand)
Re: Python - [info]zestyping, 2005-06-15 12:41 am UTC (Expand)
Re: Python - [info]regebro.wordpress.com, 2008-03-21 10:06 am UTC (Expand)
Mathematica
r6
2005-08-18 03:00 pm UTC (link)

I just started reading section 9 of A Review of Mathematica.

In[1]:= p=314159265358979323;
        q=314159265358979323.;
        r=314159265358979323.0000000000000000000;
        s=p+0.00000000000000000000;

In[2]:= {Tan[s],N[Tan[p]],Tan[q],Tan[r]}

Out[2]= {1.59981, 1.59981, ComplexInfinity, -1.1297926523089085443}

In[3]:= {p==q, q==r, r==s, r==p}

Out[3]= {True, True, True, True}

(Reply to this)


(Anonymous)
2005-09-05 07:14 am UTC (link)
In fairness, it is a SCRIPTING language, not a programming language, because it was made to solve a specific problem ie. web problems (and probably because it isn't very "politically correct").
Furthermore, the PHP manual already explained that behavior, so i don't see what's the fuss...
Maybe it shouldn't be taught, but it sure as hell is easy to learn (especially when you need something up and running right away).

(Reply to this)(Thread)(Expand)

(no subject) - [info]lexyeevee, 2007-12-11 09:57 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 12:36 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 01:29 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 01:39 am UTC (Expand)
(no subject) - [info]zestyping, 2007-12-12 02:29 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 04:50 am UTC (Expand)
(no subject) - (Anonymous), 2007-12-17 01:17 pm UTC (Expand)
(no subject) - [info]lexyeevee, 2007-12-12 04:53 pm UTC (Expand)
or why reading /should/ be taught ...
[info]sinf
2007-12-11 10:43 pm UTC (link)
Isn't this more an example of why programmers should learn the very basics of a language before using it?

How comparison works in PHP is very well described in the documentation:
http://www.php.net/manual/en/language.operators.comparison.php

(Reply to this)(Thread)(Expand)

Re: or why reading /should/ be taught ... - [info]arthurdenture, 2007-12-12 01:13 am UTC (Expand)
Re: or why reading /should/ be taught ... - [info]lexyeevee, 2007-12-12 04:18 pm UTC (Expand)
Re: or why reading /should/ be taught ... - (Anonymous), 2008-01-15 01:38 am UTC (Expand)
Type casting
(Anonymous)
2007-12-11 10:57 pm UTC (link)
I like this one also:

<?php
$a = "string";

var_dump((bool) $a);
var_dump((int) $a);
var_dump((int) (bool) $a);
?>

Output:
bool(true)
int(0)
int(1)

I *remember* having been bitten by if ($foo == 0) { ..
I remember it *well*..

(Reply to this)(Thread)(Expand)

Re: Type casting - (Anonymous), 2007-12-12 12:01 am UTC (Expand)

[info]garthnak
2007-12-11 11:02 pm UTC (link)
$ cat equality.php
<?php

$a = 0;
$b = "eggs";
$c = "spam";

print ($a === $b) ? "a === b\n" : "a !== b\n";
print ($b === $c) ? "b === c\n" : "b !== c\n";
print ($a === $c) ? "a === c\n" : "a !== c\n";
print ($a === $d) ? "a === d\n" : "a !== d\n";
print ($b === $d) ? "b === d\n" : "b !== d\n";
print ($c === $d) ? "c === d\n" : "c !== d\n";

?>

$ php equality.php
a !== b
b !== c
a !== c
PHP Notice: Undefined variable: d in /home/garthnak/equality.php on line 10
a !== d
PHP Notice: Undefined variable: d in /home/garthnak/equality.php on line 11
b !== d
PHP Notice: Undefined variable: d in /home/garthnak/equality.php on line 12
c !== d

$

(Reply to this)


[info]beverlyhillscop
2007-12-11 11:30 pm UTC (link)
This post would be more aptly titled "Why I should learn PHP."

(Reply to this)(Thread)(Expand)

Nice - (Anonymous), 2007-12-12 02:33 am UTC (Expand)
Programming languages aren't all the same. - (Anonymous), 2007-12-12 09:25 am UTC (Expand)
Re: Programming languages aren't all the same. - [info]zestyping, 2007-12-12 11:44 am UTC (Expand)
Re: Programming languages aren't all the same. - (Anonymous), 2008-01-01 12:35 pm UTC (Expand)
Re: Programming languages aren't all the same. - (Anonymous), 2008-02-02 08:34 pm UTC (Expand)
Re: Programming languages aren't all the same. - [info]lexyeevee, 2007-12-12 04:38 pm UTC (Expand)
Many more reasons why PHP should not be
(Anonymous)
2007-12-12 03:35 am UTC (link)
are well explained in this post:

http://toykeeper.net/soapbox/php_problems/

(Reply to this)

GNU Emacs
[info]vityok
2007-12-12 10:29 am UTC (link)
Oh my, GNU Emacs handles this case much more elegantly:
(setq var-a 10)
(setq var-b "eggs")
(setq var-c "spam")

(eq var-a var-b) ; nil
(eq var-b var-c) ; nil
(eq var-a var-c) ; nil
;; var-d is a void variable, does not work
(eq var-a var-d)
(eq var-b var-d)
(eq var-c var-d)

(Reply to this)


[info]hotgiraffe
2007-12-12 01:46 pm UTC (link)
actually, Java (starting from 1.5) shows just such a non-transitive behaviour when auto-unboxing is involved
public class A {

  public static void main(String[] args) {
    int a = 3;
    Integer b = new Integer(3);
    Integer c = new Integer(3);

    System.out.println(a == b ? "a == b" : "a != b");
    System.out.println(a == c ? "a == c" : "a != c");
    System.out.println(b == c ? "b == c" : "b != c");
  }
}
prints
a == b
a == c
b != c

(Reply to this)(Thread)(Expand)

(no subject) - [info]zestyping, 2007-12-12 08:39 pm UTC (Expand)
(no subject) - [info]naikrovek, 2008-01-02 08:35 pm UTC (Expand)
(no subject) - [info]hotgiraffe, 2008-01-03 01:06 pm UTC (Expand)

[info]lexyeevee
2007-12-12 05:04 pm UTC (link)
So.. I have to ask, was this dugg or something? Someone linked me here and it took me a while to notice the 2005 date.

(Reply to this)(Thread)(Expand)

javascript - (Anonymous), 2007-12-12 05:51 pm UTC (Expand)
My Fault - [info]roconnor.myopenid.com, 2007-12-12 08:27 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-14 12:38 pm UTC (Expand)
javascript
(Anonymous)
2007-12-12 05:52 pm UTC (link)
for something like this in javascript see:
http://laurens.vd.oever.nl/weblog/ontheedge/boolean/boolean.html

(Reply to this)

Another weird scenario in PHP
(Anonymous)
2007-12-12 08:35 pm UTC (link)
I recently posted a blog entry about C# vs PHP and gave an example in PHP as to how it adds numbers and strings. You can find it here.

http://keithelder.net/blog/archive/2007/12/05/Using-Regular-Expressions-in-C-vs-PHP.aspx

As I state in the article, when I taught PHP, no one ever could predict the outcome of that example. It just isn't logical.

(Reply to this)

It's the truth table.
(Anonymous)
2007-12-13 05:08 am UTC (link)
This is all 'perfectly reasonable' according to PHP's truth table.

http://www.php.net/manual/en/types.comparisons.php

When I used PHP I hated this. Because developers would use PHP's truth junk in conditionals and if you couldn't memorise it you'd have to go look it up.

Also if you didn't memorise it you'd get unexpected errors sometimes when people'd put values like 0 into forms.

I work almost exclusively in Ruby now, and I love Ruby's discernment of truth: Anything that is nil or false is false, everything else is true. Easy to remember.

I also like Python, but I can't remember what it's truth table was, I recall it was also quite simple :)

Ryan.
http://yeahnah.org

(Reply to this)


[info]sloot
2007-12-14 12:33 pm UTC (link)
I agree with you.. up to the === operator.

I assume the original posting was written before the === operator was released.

(Reply to this)

Wrong operator
(Anonymous)
2008-01-01 01:49 pm UTC (link)
That's misleading, because you're using the wrong operators -- retry your test using the proper '===' and '!==' operators, and you'll see the correct result.

(Reply to this)

Your post is sloppy
(Anonymous)
2008-01-01 04:44 pm UTC (link)
Seriously, this post is plain sloppy. If you don't know the difference in PHP between the '==' and '===' operators then you shouldn't be making sweeping arguments for or against the language.

See John Lim's more detailed retort:
http://phplens.com/phpeverywhere/?q=node/view/244

(Reply to this)


(Anonymous)
2008-01-01 06:04 pm UTC (link)
I don't understand something, so it shouldn't exist! Waaah!

If you're just starting in computer science you're in for a lifetime of disappointment son.

(Reply to this)


Page 1 of 2
<<[1] [2] >>

Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…