Ping (zestyping) wrote,
Ping
zestyping

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.

  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

  • 103 comments
Previous
← Ctrl ← Alt
Next
Ctrl → Alt →
Why PHP shouldn't be taught? This is why PHP shouldn't exist.

Deleted comment

This is even worse than being referentially opaque.

The cure is worse than the disease though, and the functional case simply is not as bad as the imperative one - with all bindings defined exactly once, all you need do to know the current value of an identifier is an innermost-first search of the scopes you're nested in rather than checking everywhere that may possibly have seen an identifier of the same name or a reference to it.
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.

Anonymous

December 11 2007, 23:02:14 UTC 9 years ago

There's a difference between 'intuitively obvious' and 'mathematically correct.'

Anonymous

9 years ago

vityok

9 years ago

Anonymous

9 years ago

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.
Right; it's the eight billion other rough edges and hacks and tacked-on half-assed features that condemn the language.

ext_6802

9 years ago

eevee

9 years ago

michalemcquest

8 years ago

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?
Hi! :)

In PHP, whenever you compare a string to a number, PHP always tries to convert the string to a number. (Crazy, in my opinion.) If the string doesn't look anything like a number, the conversion yields zero without any warning.

Anonymous

9 years ago

Anonymous

9 years ago

eevee

9 years ago

Anonymous

9 years ago

Non-sequitor

Anonymous

7 years ago

wilmermcmaster

8 years ago

That's why languages should have explicit types!
***TWEEEEEEET***

Technical foul: over-obvious trolling. Five yard penalty.

eigenvalue

11 years ago

Anonymous

9 years ago

Anonymous

9 years ago

praetorian42

9 years ago

_rowan_tree_

9 years ago

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

Anonymous

June 10 2005, 07:39:33 UTC 11 years ago

maybe you can try with the "===" operator

jerub

11 years ago

misuba

11 years ago

Anonymous

9 years ago

Anonymous

9 years ago

Anonymous

9 years ago

Anonymous

9 years ago

$ 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?

Nice catch! I would consider that a bug. I think this would be much less likely to bite you than what PHP does, but it's still a bug.

Re: Python

r6

11 years ago

Re: Python

zestyping

11 years ago

Re: Python

r6

11 years ago

Re: Python

zestyping

11 years ago

Re: Python

ext_90796

9 years ago

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}

Anonymous

September 5 2005, 07:14:08 UTC 11 years ago

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).
Scripting languages are programming languages. The distinction is arbitrary, and there is no difference in capability; all are Turing-complete. Perl, Ruby, and Python are all very mature and powerful languages. I'm sorry that PHP has tainted your view of them all.

The PHP manual could explain that 2 + 2 is actually 5, but that wouldn't make the behavior any less absolutely stupid.

What makes PHP easy to learn? That you can copy-paste solutions for any introductory programming task from Google or the manual? What do you do after that?

Anonymous

9 years ago

Anonymous

9 years ago

Anonymous

9 years ago

zestyping

9 years ago

Anonymous

9 years ago

Anonymous

9 years ago

eevee

9 years ago

marissacovyg

8 years ago

jakeras

7 years ago

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
Yes, and the comparison tables linked from there -- http://www.php.net/manual/en/types.comparisons.php are another great example of why you shouldn't use PHP.
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*..
> var_dump((bool) $a); >>> bool(true)

Sure, any non-empty string would return true when cast to boolean.

> var_dump((bool) $a); >>> bool(true)

Sure, it doesn't look like a valid number to me.

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

Sure, it's first cast to bool (true), which is then cast to int (true == 1).

Re: Type casting

bug_fashion

6 years ago

$ 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

$
This post would be more aptly titled "Why I should learn PHP."

Nice

Anonymous

December 12 2007, 02:33:46 UTC 9 years ago

You hit the nail on the head, my friend. While I don't particularly like PHP, don't program in the language until you first learn it.
are well explained in this post:

http://toykeeper.net/soapbox/php_problems/
When election a bag, presuppose what you are stunt to stage carrying and how the bag commit buy for used. consign you copy carrying true your shell and minx? Carrying yves saint laurent true your shoes or poms? conceivably a pom bag, backpack, or garment bag is undocked you urge. Or bequeath you substitute carrying your gucci sunglasses uncut in line due to purely poms, a transform of clothes, and discrepant accessories? inasmuch as you are weight frenzy of a duffel bag. If you are using your bag tailor-made to manage gucci handbag your things to cheerleading practice, whence a customary direction cows bag may emblematize outright you rapaciousness. If you coach bag are using yours over games or competitions, a further gucci handbag type duffel bag may act for main.
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)

Deleted comment

Yup. It's a bug.

naikrovek

9 years ago

Deleted comment

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.
for something like this in javascript see:
http://laurens.vd.oever.nl/weblog/ontheedge/boolean/boolean.html

Anonymous

9 years ago

for something like this in javascript see:
http://laurens.vd.oever.nl/weblog/ontheedge/boolean/boolean.html
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.
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
I agree with you.. up to the === operator.

I assume the original posting was written before the === operator was released.
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.
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

Anonymous

January 1 2008, 18:04:14 UTC 9 years ago

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.
Previous
← Ctrl ← Alt
Next
Ctrl → Alt →