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.

Subscribe
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 101 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 10 years ago

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

Anonymous

10 years ago

vityok

10 years ago

Anonymous

10 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

10 years ago

eevee

10 years ago

michalemcquest

9 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

10 years ago

Anonymous

10 years ago

eevee

10 years ago

Anonymous

10 years ago

Non-sequitor

Anonymous

8 years ago

wilmermcmaster

9 years ago

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

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

eigenvalue

12 years ago

Anonymous

10 years ago

Anonymous

10 years ago

praetorian42

10 years ago

_rowan_tree_

10 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 12 years ago

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

jerub

12 years ago

misuba

12 years ago

Anonymous

10 years ago

Anonymous

10 years ago

Anonymous

10 years ago

Anonymous

10 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

12 years ago

Re: Python

zestyping

12 years ago

Re: Python

r6

12 years ago

Re: Python

zestyping

12 years ago

Re: Python

ext_90796

10 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 12 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

10 years ago

Anonymous

10 years ago

Anonymous

10 years ago

zestyping

10 years ago

Anonymous

10 years ago

Anonymous

10 years ago

eevee

10 years ago

marissacovyg

9 years ago

jakeras

8 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.

Re: or why reading /should/ be taught ...

Anonymous

10 years ago

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

7 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 10 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.

Programming languages aren't all the same.

Anonymous

10 years ago

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