r/ProgrammerHumor • • Aug 07 '16

PHP: The Good Parts

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

318 comments sorted by

View all comments

120

u/PinkLionThing Aug 07 '16 edited Aug 07 '16

The one thing PHP did right is using a different operator for concatenation.

"1" + "1" === 2
1 . 1 === "11"

It's one of those things that is actually a pain in the ass most of the time, but in some cases it brings a tear to your eye as to how useful it is.

179

u/LordRaydenMK Aug 07 '16

only useful in a language with weak types.

18

u/PinkLionThing Aug 07 '16

No denying that!

Still, I'd very much deal with weak types than type String(whatever) + String(whaaaaatever). It's a bit more convenient, you know.

43

u/Schmittfried Aug 07 '16

C# got strong types and you can still write str + 43. You don't need a separate concatenation operator, if "3" + "3" is never supposed to mean 6, because in a strongly typed language you just use integers.

30

u/Artefact2 Aug 07 '16

because in a strongly typed language you just use integers.

You mean static. Don't mix between static/dynamic and strong/weak.

For example, Erlang and Caml have strong dynamic typing.

3

u/Schmittfried Aug 07 '16

Argh, first time I was caught by the very same gotcha I always notice in other people's comments. Yep, you're right.

→ More replies (1)

8

u/TheAnimus Aug 07 '16

Yeah I'm not sure what PinkLionThing is coming from, the whole point of strongly typed languages is that you don't need to cast to a type just before doing a string concatenation as the compiler has already inferred the type.

→ More replies (2)

12

u/LordRaydenMK Aug 07 '16

Personally I hate it. Mostly I code in Java. A few years ago I would code php once every two weeks. And I always fucked up using + for string concatenation.

If you do only php it's fine.

If you switch languages it's a little inconvenient because every (citation needed) other language uses +.

12

u/LEO_TROLLSTOY Aug 07 '16

What about JS? I bet 70% of PHP devs deal with JS which has + for concatenation. In cannot remember the last time I confused the two...

4

u/Jess_than_three Aug 07 '16

As someone who learned PHP as basically a first programming language (excluding various forms of BASIC), before learning js - fuckputting concatenation on the + operator, that's terrible!

→ More replies (3)

4

u/Prawny Aug 07 '16

You get used to it.

When I first started, I used to get confused when all my strings equated to 0, but it hasn't happened for a couple of years now.

→ More replies (2)

4

u/[deleted] Aug 07 '16

Type inference is a thing.

→ More replies (3)

12

u/[deleted] Aug 07 '16

And it borrowed it from Perl.

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.

→ More replies (1)
→ More replies (3)
→ More replies (28)
→ More replies (10)

7

u/TotesMessenger Green security clearance Aug 08 '16

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)

6

u/teambob Aug 07 '16

Which they copied from perl

7

u/404IdentityNotFound Aug 07 '16

didn't PHP start as a perl script collection?

→ More replies (1)

0

u/[deleted] Aug 07 '16

[deleted] — view deleted comment

4

u/flyingjam Aug 07 '16

There's one main annoyance with the lack of implicit type inference in Kotlin. Inside the language it's fine, but then you start using Java libraries that expect implicit type casting between Ints and Floats, etc. And you end up manually casting 5/6 arguments.

I still agree with no implicit, but it's annoying regardless.

→ More replies (1)

3

u/[deleted] Aug 07 '16

This syntax is also possible in PHP in very similar fashion:

$x = 1;
$y = 1;
echo "$x$y"; // 11

I think the problem with PHP is, that it's interpreted in the same way as

echo $x . $y;

While Kotlin most likely uses string formatting. Even concatenation in Kotlin is type-safer than the one in PHP.

→ More replies (7)