Ping ([info]zestyping) wrote,
@ 2007-12-11 18:32:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Why PHP should never be taught, Part II.
This is a follow-up to Why PHP should never be taught.

% cat comparison.php
<?php

$a = -1;
$b = 0;
$c = 1;
$d = True;

print "d > $a: "; print ($d > $a) ? "yes\n" : "no\n";
print "d > $b: "; print ($d > $b) ? "yes\n" : "no\n";
print "d > $c: "; print ($d > $c) ? "yes\n" : "no\n";
print "\n";
print "d < $a: "; print ($d < $a) ? "yes\n" : "no\n";
print "d < $b: "; print ($d < $b) ? "yes\n" : "no\n";
print "d < $c: "; print ($d < $c) ? "yes\n" : "no\n";
print "\n";
print "e > $a: "; print ($e > $a) ? "yes\n" : "no\n";
print "e > $b: "; print ($e > $b) ? "yes\n" : "no\n";
print "e > $c: "; print ($e > $c) ? "yes\n" : "no\n";
print "\n";
print "e < $a: "; print ($e < $a) ? "yes\n" : "no\n";
print "e < $b: "; print ($e < $b) ? "yes\n" : "no\n";
print "e < $c: "; print ($e < $c) ? "yes\n" : "no\n";
?>

% php comparison.php 
d > -1: no
d > 0: yes
d > 1: no

d < -1: no
d < 0: no
d < 1: no

e > -1: no
e > 0: no
e > 1: no

e < -1: yes
e < 0: no
e < 1: yes
%
Aaaarghhh!!! It's so arbitrary.


(Post a new comment)


[info]stellae
2007-12-12 02:45 am UTC (link)
That just made my head hurt.

And I totally thought I was getting better at the whole programming thing...

(Reply to this)(Thread)


[info]zestyping
2007-12-12 04:27 am UTC (link)
It's not you, it's PHP. It's a broken language. Avoid it whenever possible.

(Reply to this)(Parent)(Thread)(Expand)

(no subject) - [info]vityok, 2007-12-12 06:57 am UTC (Expand)
wow.. - (Anonymous), 2007-12-12 10:14 pm UTC (Expand)
Re: wow.. - [info]two_pi_r, 2007-12-13 03:33 am UTC (Expand)
Wrong Conclusion - (Anonymous), 2007-12-19 04:58 pm UTC (Expand)
Re: Wrong Conclusion - (Anonymous), 2008-01-22 09:52 pm UTC (Expand)

[info]ephermata
2007-12-12 03:00 am UTC (link)
Sorry if this is missing the point, but where do you assign to $e ? or is the point that $e is never assigned to?

(Reply to this)(Thread)


[info]poliphilus
2007-12-12 03:22 am UTC (link)
Yeah, in PHP you can generally use undefined variables to mean, depending on the context, 0, '', FALSE, NULL, etc. (If I remember correctly.)

I also heard that if you pass an undefined variable as the second argument to preg_match, and it's the second Wednesday of the month and you were born in New Zealand, it is interpreted as the string "RETARBITRARY DESIGN DECISION". And it is said that if you press the right combination of keys as the program runs, you will get an extra life and a warp whistle.

(Reply to this)(Parent)(Thread)(Expand)

(no subject) - [info]rubrick, 2007-12-12 08:24 am UTC (Expand)
(no subject) - [info]flipzagging, 2007-12-12 10:32 am UTC (Expand)
(no subject) - [info]two_pi_r, 2007-12-12 07:14 pm UTC (Expand)
Error level - (Anonymous), 2007-12-12 09:35 am UTC (Expand)
Re: Error level - [info]flipzagging, 2007-12-12 10:37 am UTC (Expand)
Re: Error level - [info]two_pi_r, 2007-12-12 07:29 pm UTC (Expand)
Re: Error level - [info]devsoma.blogspot.com, 2007-12-12 08:29 pm UTC (Expand)
Re: Error level - [info]two_pi_r, 2007-12-12 08:58 pm UTC (Expand)
Re: Error level - [info]devsoma.blogspot.com, 2007-12-13 12:24 pm UTC (Expand)
Re: Error level - [info]two_pi_r, 2007-12-13 05:03 pm UTC (Expand)
Re: Error level - (Anonymous), 2007-12-14 01:02 am UTC (Expand)
Re: Error level - [info]devsoma.blogspot.com, 2007-12-14 01:03 am UTC (Expand)
Re: Error level - [info]two_pi_r, 2007-12-14 01:10 am UTC (Expand)
Re: Error level - [info]devsoma.blogspot.com, 2007-12-14 12:19 pm UTC (Expand)
Re: Error level - [info]two_pi_r, 2007-12-14 06:24 pm UTC (Expand)
Re: Error level - [info]devsoma.blogspot.com, 2007-12-14 06:58 pm UTC (Expand)
Re: Error level - [info]two_pi_r, 2007-12-14 07:11 pm UTC (Expand)
Re: Error level - [info]lexyeevee, 2007-12-14 07:15 pm UTC (Expand)
Re: Error level - [info]lexyeevee, 2007-12-14 06:48 pm UTC (Expand)
Re: Error level - [info]fusiongyro, 2007-12-15 08:50 am UTC (Expand)
Re: Error level - [info]lexyeevee, 2007-12-14 01:26 am UTC (Expand)
Re: Error level - [info]devsoma.blogspot.com, 2007-12-14 12:31 pm UTC (Expand)
Re: Error level - [info]lexyeevee, 2007-12-14 04:11 pm UTC (Expand)
Re: Error level - [info]lexyeevee, 2007-12-13 05:38 pm UTC (Expand)
(no subject) - [info]two_pi_r, 2007-12-12 07:07 pm UTC (Expand)

[info]cuthalion
2007-12-12 03:20 am UTC (link)
As a C++ programmer I have to say: We've also got plenty of awful weird stuff like that if you go looking for it.

(Reply to this)(Thread)


[info]cpirate
2007-12-12 04:34 am UTC (link)
That's the real problem with PHP, the weird stuff usually comes looking for you. At least when you're doing something strange in C++, you're usually well aware.

(Reply to this)(Parent)(Thread)(Expand)

Well that depends - (Anonymous), 2007-12-12 09:31 am UTC (Expand)
Re: Well that depends - [info]lexyeevee, 2007-12-12 05:06 pm UTC (Expand)
Re: Well that depends - [info]frig_neutron, 2008-05-22 06:39 pm UTC (Expand)
Re: Well that depends - (Anonymous), 2008-01-04 09:49 pm UTC (Expand)
So?
[info]mr_privacy
2007-12-12 05:36 am UTC (link)
What language should be taught, and in what language can you not write bad code?

I agree, PHP is ugly. This is an ugly anecdote.

(Reply to this)(Thread)

Re: So?
[info]vityok
2007-12-12 06:59 am UTC (link)
We were taught Pascal as an intro to imperative languages, and C as a systems programming language, C++ and little Java as OO languages, Lisp and Prolog as specialized languages.

(Reply to this)(Parent)(Thread)(Expand)

Re: So? - (Anonymous), 2007-12-12 09:56 am UTC (Expand)
Re: So? - [info]vityok, 2007-12-12 10:23 am UTC (Expand)
Re: So?
[info]lexyeevee
2007-12-12 05:07 pm UTC (link)
Although I am far more a Larry guy than a Guido guy, Python seems easy for anyone to pick up.

(Reply to this)(Parent)(Thread)(Expand)

Re: So? - [info]two_pi_r, 2007-12-12 07:25 pm UTC (Expand)
This could go on for some time...
[info]http://openid.aol.com/ironfroggy
2007-12-12 05:56 am UTC (link)
An entire blog of only this could be easily kept going.

(Reply to this)


[info]whumpdotcom
2007-12-12 07:32 am UTC (link)
Lisp is elegant. PHP is ubiquitous.

Ruby is elegant as well, but has been tainted by the awful stain of Rails.

(Reply to this)(Thread)(Expand)

(no subject) - [info]vityok, 2007-12-12 10:33 am UTC (Expand)
(no subject) - [info]etfb, 2007-12-12 11:41 pm UTC (Expand)
(no subject) - [info]vityok, 2007-12-13 06:48 am UTC (Expand)

(Anonymous)
2007-12-12 10:53 am UTC (link)
Thats great, now find me a real world example where PHP's type handling could cause unavoidable problems.

(Reply to this)(Thread)(Expand)

(no subject) - [info]lexyeevee, 2007-12-12 05:08 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 05:24 pm UTC (Expand)

(Anonymous)
2007-12-12 01:07 pm UTC (link)
So if you compare a boolean value with an integer or compare an uninitialized value to anything, you might run in to trouble? Thats a real shock...

(Reply to this)


[info]bhagany
2007-12-12 02:20 pm UTC (link)
Seriously. Learn the language, and stop being an idiot. All of this is documented, and is a result of type coercion. The solution? Don't write stupid code.

(Reply to this)(Thread)(Expand)

(no subject) - [info]lexyeevee, 2007-12-12 05:10 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-12 05:22 pm UTC (Expand)
(no subject) - [info]lexyeevee, 2007-12-12 05:27 pm UTC (Expand)
(no subject) - [info]two_pi_r, 2007-12-12 07:15 pm UTC (Expand)
You're a stupid fucking twat.
(Anonymous)
2007-12-12 04:02 pm UTC (link)
Just because you're not able to cope with the fact that comparing boolean, non-existant, and integer values is NEVER going to give you meaningful data, doesnt mean PHP is a bad language. You're a total fucking idiot, dont bother coding in anything again, you're clearly too braindead to do it.

(Reply to this)(Thread)(Expand)

Re: You're a stupid fucking twat. - [info]the_fury, 2007-12-12 04:14 pm UTC (Expand)
Re: You're a stupid fucking twat. - [info]leech, 2007-12-12 04:48 pm UTC (Expand)
Re: You're a stupid fucking twat. - [info]lexyeevee, 2007-12-12 05:10 pm UTC (Expand)
Why Allow Meaningless Constructs? - [info]digitalsidhe, 2007-12-12 06:36 pm UTC (Expand)
Re: Why Allow Meaningless Constructs? - [info]lexyeevee, 2007-12-12 06:49 pm UTC (Expand)
Re: Why Allow Meaningless Constructs? - (Anonymous), 2008-01-01 12:22 pm UTC (Expand)
Re: Why Allow Meaningless Constructs? - [info]lexyeevee, 2008-01-01 12:32 pm UTC (Expand)
Re: You're a stupid fucking twat. - [info]two_pi_r, 2007-12-12 07:22 pm UTC (Expand)
Re: You're a stupid fucking twat. - [info]lexyeevee, 2007-12-13 05:43 pm UTC (Expand)
Re: You're a stupid fucking twat. - [info]etfb, 2007-12-12 11:43 pm UTC (Expand)
more flame?
[info]bsittler
2007-12-12 05:30 pm UTC (link)
yeah, coerced types in php behave very oddly. the signed/unsigned bugs can be particularly tricky to track down. far worse, though, are the bugs around the object-orientation hackssyntax (whose semantics differ wildly in different php versions, while nonetheless using "compatible" syntax!)

of course the globals-from-the-client fiasco is *still* causing security problems, so that's probably php's single greatest flaw.

the lack of exception handling and the extremely limited and buggy standard library also lead to extra bugs and trickier debugging.

while i have written a bit of php, i do not recommend it to anyone. use python, use bash, use perl, use java, but do not use php for your web development (or any other purpose.)

(Reply to this)


[info]digitalsidhe
2007-12-12 06:29 pm UTC (link)

There's nothing arbitrary about this at all. You're doing a greater-than/less-than comparison with one Boolean operand, so the other operands are coerced to Boolean first. (If you want a language that doesn't do type coercion or dynamic typing, then PHP is naturally not going to be your cup of tea. But the same could be said of lots of other dynamically typed languages.)

So, these statements are effectively asking things like:

True > False (always turns out true)
True > True (always false)
False > True (always false)
False > False (always false)

The uninitialized variable $e also evaluates as false, yielding the results you saw. You may disagree that the language should evaluate True as being "greater than" False, but I'm hard put to think of a more reasonable behavior for greater-than/less-than comparisons of Boolean values. Throwing a "type mismatch" or similar error might make sense, but it could also be argued against, on the grounds that dynamic typing is pointless if you don't use it.

(Reply to this)(Thread)(Expand)

(no subject) - [info]lexyeevee, 2007-12-12 06:56 pm UTC (Expand)
Just a note - (Anonymous), 2007-12-12 07:50 pm UTC (Expand)
Re: Just a note - [info]lexyeevee, 2007-12-12 07:57 pm UTC (Expand)
Re: Just a note - [info]two_pi_r, 2007-12-12 08:06 pm UTC (Expand)
(no subject) - (Anonymous), 2007-12-14 11:12 am UTC (Expand)
Search Trees
[info]roconnor.myopenid.com
2007-12-13 09:51 am UTC (link)
That must make sorting lists and building search trees a real pain.

(Reply to this)(Thread)(Expand)

I don't think you'd get to make search trees - (Anonymous), 2007-12-17 01:02 pm UTC (Expand)
Re: I don't think you'd get to make search trees - (Anonymous), 2008-01-02 08:32 am UTC (Expand)
Th from Brazil
(Anonymous)
2007-12-14 11:14 am UTC (link)
I found this stupid post on the net... and can't go away without comment...

Yeah...
There are so many bad programmers... like these that need to teach the processor the language! :P

Marvelous argument, Digital Sidhe. This is for who understands php... not for an asp or java prisioner...

zestyping... I guess that you need to study more logic! And new languages too! :P

(Reply to this)(Thread)(Expand)

Re: Th from Brazil - [info]two_pi_r, 2007-12-15 12:50 am UTC (Expand)
Re: Th from Brazil - (Anonymous), 2007-12-17 12:49 pm UTC (Expand)
Just no.
(Anonymous)
2007-12-17 08:58 pm UTC (link)
It is not PHP's fault that you fail at programming.

The extend of what you are doing is saying "PHP should not be taught cause I am too inexperienced to handle variable types."

Lets break it down, shall we?
In the first line, print "d > $a: "; print ($d > $a) ? "yes\n" : "no\n"; , you are trying to compare a BOOLEAN with a INTEGER. Now, I will refrain from elaborating how incredibly retarded a comparison like this is.. and move on to the facts:

To compare the Boolean "True" to the numbers, it tries to "translate" the INTEGER numbers to BOOLEAN values for this comparison. (int)-1 turns to (bool)True. In fact, any set variable that is not 0 or empty always equals to (bool)True while a variable that is not set, has no content ( empty ) or just the content "0" ( doesn't matter whether its the number 0 or a string "0" ) always equals to (bool)False. ( And for your information, most languages handle it like this, not just PHP. )

Now, with that knowledge, lets look at your comparisons again. Also ,we should keep in mind that True is considered larger than False.

Additionaly I'd like to point out ( again ) that variables that have not been
declared are also counted as (bool)False.

On to the REAL comparison ( i.e. whats going on for PHP ):
True > True: No, since obviously they are the same.
True > False: Yes, since True is considered larger than False.
True > True: No, again. If it was >= this would consider to yes, but it is not.

True < True: No.
True < False: No.
True < True: No.

False > True: Wrong. As false is not considered more than True.
False > False: Nope, theyre the same.
False > True: Again, nope.

False < True: Yep, True is larger than False
False < False: Nope.
False < True: Again, yep.

As you can see, it makes perfect sense and is quite logical.

I suggest you actually LEARN the language before trying to libel it.

Besides, the actual comparison that you were expecting ( on a int level ) you can easily obtain by changing the line "$d = True;" to "$d = (int)True; $e = (int)False;".

Now, please stop spreading your ignorance and learn what you are trying to "defeat".

(Reply to this)(Thread)(Expand)

Re: Just no. - [info]zestyping, 2007-12-17 11:22 pm UTC (Expand)
Re: Just no. - (Anonymous), 2008-01-01 04:47 am UTC (Expand)
Re: Just no. - (Anonymous), 2008-01-02 08:38 am UTC (Expand)
Re: Just no. - (Anonymous), 2008-01-02 02:54 pm UTC (Expand)
Don't insult other languages - [info]nk.myopenid.com, 2007-12-18 03:47 am UTC (Expand)

[info]aegisknight
2007-12-19 07:12 am UTC (link)
haha, I know. At IMVU, we have a file called test_php.php that codifies in unit tests the arbitrary and ridiculous behavior of this language. Some gems:

$this->assertTrue(new EmptyClass == NULL);

$array = false;
$array[] = 42;
$this->assertIdentical(array(42), $array);

Apparently the internal malloc implementation takes a float too. :P

(Reply to this)(Thread)(Expand)

(no subject) - (Anonymous), 2008-01-02 02:50 pm UTC (Expand)
(no subject) - [info]aegisknight, 2008-01-02 02:52 pm UTC (Expand)
Are you sure? - (Anonymous), 2008-01-02 03:04 pm UTC (Expand)
Re: Are you sure? - [info]aegisknight, 2008-01-02 03:07 pm UTC (Expand)
Re: Are you sure? - (Anonymous), 2008-01-02 03:10 pm UTC (Expand)
Re: Are you sure? - [info]aegisknight, 2008-01-02 03:13 pm UTC (Expand)
Re: Are you sure? - (Anonymous), 2008-01-02 03:23 pm UTC (Expand)
Re: Are you sure? - [info]mernen.myopenid.com, 2008-01-15 01:33 am UTC (Expand)
Agree, but what do we do?
(Anonymous)
2008-01-04 05:53 pm UTC (link)
I have enough programming background to know that I'm terrified of PHP for reasons like you've pointed out. Maybe I was just ruined by my alma mater's horrible, horrible PHP and ASP classes. What should I use? Is there an alternative?

(Reply to this)

Not many surprises for a seasoned programmer
(Anonymous)
2008-01-06 05:32 pm UTC (link)
I have hardly written any PHP code worth mentioning in my life, and yet, as to a reasonably experienced programmer, it is quite obvious to me what's going on.

In the former example you compare 'string' and 'null' to 'integer'. The string and the null are being coerced into an integer, and the result of these coercions is 0. Then everything makes sense.

In this example you compare 'integer' and 'null' to 'boolean'. The integers and the nulls are being coerced into boolean values. Zero integer values and null are coerced to False, and non-zero integer values are coerced to True. True is greater than False, False is lower than True. Then everything makes sense.

Not many surprises here for a seasoned programmer. Understanding what happens under the hood is something that new developers need to learn. I can scarcely fathom how people can be successful developers without learning to understand the software they use.

Users don't have to do that. But programmers do.

denis bider (http://denisbider.blogspot.com/)

(Reply to this)

zestyping ... you're an idiot. I rest my case.
(Anonymous)
2008-01-25 10:50 pm UTC (link)
Why?

How about this comparison ... you can stick it in any language you want even your asss...
try this piece of code on any compiler....

if ("zestyping" > "all idiots around the world") {
echo "It's true, on all compilers";
}
...wow...I'm amazed ... it returns true, they should stop you from touching the keyboard.
OMFG.

(Reply to this)(Thread)(Expand)

zestyping ... you're an idiot. I rest my case. Part 2 - (Anonymous), 2008-02-18 12:14 pm UTC (Expand)
zestyping ... you're an idiot. I rest my case. Part 2
(Anonymous)
2008-02-18 12:15 pm UTC (link)
if(IQcheck('zestyping')>=1) echo "I is wrekin I smart";
elseif(IQcheck('zestyping')<=0) echo "Ya Try learning 1 programming language before your post noob shit";
else echo "PHP is a powerful thing if used right";

//If you didnt already know IQcheck is not a normal PHP function
//Its made by the programmer try Google its a great tool for learning

(Reply to this)


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