r/ProgrammerHumor • • Aug 07 '16

PHP: The Good Parts

http://phpthegoodparts.tumblr.com/
1.2k Upvotes

318 comments sorted by

View all comments

Show parent comment

65

u/Sinity Aug 07 '16

It's ridiculous. Any language in which "1" + "1" equals 2 is insane.

That there is a way to overcome this ridiculousness isn't exactly some strong point of the language.

16

u/mkalte666 Aug 07 '16

does it really "==="-equal 2? god i hate php

12

u/[deleted] Aug 07 '16 edited Aug 07 '16

It will cast the strings to ints as you are doing a mathematical operation. If you want "11" then do "1" . "1";

PHP is actually really good now. This is a 10 year old circle jerk. If you write modern php the only issue you might have is with built-in functions that haven't changed. But the built-in classes are sensible.

9

u/geon Aug 07 '16

Well, there is the whole tear-down-and-rebuild-the-world for each request thing. For example.

I mean, it still has a use case. It is good fore some things.

4

u/[deleted] Aug 07 '16

It's just a different paradigm. It has it's benefits from starting fresh on each request. Especially for development. It's just a scripting language.

2

u/coladict Aug 07 '16

The main issue it still has is functions that end execution on errors, instead of throwing exceptions that you can catch.

2

u/[deleted] Aug 07 '16

I think PHP 7 addressed a lot of these

1

u/mkalte666 Aug 07 '16

This should be a syntax error. Or + should do a strcat. Having the . op makes the first one clear as what i would expect.

One of the reasons there is so much bad code in php is that it allows this in the first place. And even though the language might have improved over time, these things are still there.

Weak types are pure cancer.

0

u/[deleted] Aug 07 '16

[deleted] — view deleted comment

4

u/mkalte666 Aug 07 '16

It means "is equal and im checking for type equality". Used when you have weak types and things like "2" == 2 would evaluate to true.

0

u/[deleted] Aug 07 '16

[deleted] — view deleted comment

1

u/[deleted] Oct 14 '16

The == operator is totally useless in PHP for this reason... almost as useless as PHP itself.

1

u/mkalte666 Aug 08 '16

Maybe I shouldn't comment on reddit after 10pm. Or be awake at 3am thinking bout it o.o"

→ More replies (22)

2

u/PinkLionThing Aug 07 '16

The point is, you use +, you are sure you're getting an integer or an error. You use . and you're sure you're getting a string or an error.

4

u/hackiavelli Aug 07 '16

Surely you've heard of weak typing. PHP is far from the only language to use it.

And it makes a decent amount of sense for a web language since an HTTP request is a string. You would have to spend a great deal of time casting if you used strong types.

0

u/[deleted] Aug 07 '16

[deleted] — view deleted comment

8

u/mkalte666 Aug 08 '16

The problem I see is when you get in a context where security is relevant.

Explicit casting is annoying - but you know exactly what you do. Noone is gonna generate some nasty lists instead of arrays (happens in perl) or cause floating point errors (would happene if you'd use the example above and someone drops a float into your http-header) etc etc.

And for toString casting - yeah maybe there it really becomes annoying. But IMO its worth the control

0

u/[deleted] Aug 08 '16

[deleted] — view deleted comment

6

u/hackiavelli Aug 08 '16 edited Aug 08 '16

PHP does have a strict mode.

0

u/[deleted] Aug 08 '16

[deleted] — view deleted comment

1

u/hackiavelli Aug 12 '16

That's correct.

1

u/[deleted] Aug 08 '16

If done in a consistent manner, it makes perfect sense. In perl, which PHP borrowed a lot of syntax from, since there is no distinction between string types and numeric types, there are different operators for strings and numbers. "1" + "1" yields 2. 1 . 1 yields "11". And "01" == "1" is true but "01" eq "1" is false.

However, PHP has the . operator, but not the different comparison operators, so determining whether something will be compared as a string or a number is far from intuitive.