Hacker News Re-Imagined

Modern PHP Cheat Sheet

  • 180 points
  • 14 hours ago

  • @rehhouari
  • Created a post
  • • 107 comments

Modern PHP Cheat Sheet


@lolspace 8 hours

Replying to @rehhouari 🎙

What is modern PHP?

Reply


@pbowyer 12 hours

Replying to @rehhouari 🎙

I'm not convinced by Enums having methods. For those of you using other languages that support this, do you find them useful?

Reply


@NaN1352 10 hours

Replying to @rehhouari 🎙

As a JS dev it’s nice to see php imitate some newer js syntax like nullish coalescing, arrow function, … helps me to switch between the languages.

It really needs a way to declare type of small data structures though, in a very concise way (no class boilerplate, for performance eg. going through 10000’s of entities).

PS : on the other hand I’m starting to dislike how languages become "designed by committee", and lose the purity of the original design.( thinking of JS mostly here with its gazillion rfcs)

Reply


@734129837261 9 hours

Replying to @rehhouari 🎙

A Reddit post from a few years ago explains pretty well why PHP has such a terrible reputation: https://www.reddit.com/r/webdev/comments/5tqoqa/web_devs_wha...

PHP should have chosen to undergo a rebranding a few years ago, if you ask me. The stigma it has to deal with is probably unsurmountable and will be forever. If you think PHP, you think WordPress and Joomla, and that means "hacks and vulnerabilities"...

It's a good language nowadays, with quirks like any other language, but I still have one big problem with it: There are too many "PHP developers" out there, and most of them are very bad, and many of the good ones are from poor regions in the world and you can find them on Fiverr doing kick-ass work for a grand total of $50.

Reply


@karmakaze 6 hours

Replying to @rehhouari 🎙

Super cool:

> Union Types - Combine several types into one union, which means that whatever input must match one of the given types: `function find(int|string $id)`

TypeScript and PHP have the distinct honor of being a popular language with ad-hoc union types (not the concrete subclass/enums that other languages seem to think are sufficient).

Reply


@gregoriol 13 hours

Replying to @rehhouari 🎙

The choice of colors for this page makes it really difficult to read

Reply


@sam_goody 12 hours

Replying to @rehhouari 🎙

The comment on Numeric values is misleading:

> Use the _ operator to format numeric values:

> $price = 100_10;

> // $100 and 10 cents

Makes it sound like the underscore replaces the decimal, which it doesn't. The underscores are simply ignored by the PHP parser, so that 10_0.1 === 1_00.1.

You could afterwards use PHP to replace the `_` with a comma for display purposes, and I assume that is what they are trying to say.

Reply


@ognarb 13 hours

Replying to @rehhouari 🎙

I like how PHP is evolving. I wouldn't say it's always a joy to use but it definitively improved a lot.

Reply


@sharmin123 13 hours

Replying to @rehhouari 🎙

Guide on The Mental Health Effects Of Extramarital Affairs: https://www.hackerslist.co/guide-on-the-mental-health-effect...

Reply


@NaN1352 10 hours

Replying to @rehhouari 🎙

Genuine question from a php hobbyist : what is the equivalent of Typescript’s ability to declare an object’s structure?

It’s really weird to me, I mean don’t we do this all the time? Work with eg. an $options array/obj passedto a constructor, or say, a message decoded from JSON…

I could write $name = $message[‘username’] … and there is no checks in ide or runtime, while the phpdoc will just document $message to be an object or array… what am I missing?

It looks like php devs create full blown classes to represent just about every data strcuture, but what if it’s just data and you don’t need any attached logic’ Isn’t there a concise way to declare a complex type?

Reply


@tlackemann 11 hours

Replying to @rehhouari 🎙

I'm using Laravel/PHP for a new project and it's been a joy. A true joy. After spending the last 6+ years writing backend services in Node, PHP feels like a breath of fresh air. No restarting the server, no compiling errors from babel/typescript wackiness, no blocking threads, server-side templates out-of-the-box, and so much more.

PHP has come a really long way, I really hope to see it's resurgence some day after everyone has been burned enough from running JavaScript as their server.

Reply


@FriedrichN 9 hours

Replying to @rehhouari 🎙

My absolute favorite thing about PHP is the arrays and the ease of working with them. Whenever I find myself working in another language dealing with loads of data I'm immediately thinking of how easy it would be to accomplish it in PHP.

And I've been loving all the new additions to PHP over the last few years as well. Version 5 was rather limited but since 7 it has grown into an actual programming language for which I feel I don't have to apologize for.

Reply


@damagednoob 11 hours

Replying to @rehhouari 🎙

The evolution of PHP has shown the difference that having engaged stewards of the language can make. Saw a list of quotes[1] from Rasmus Ledorf which I think explains it's stagnation. As someone who used to sneer at the language, modern PHP doesn't look out of place among Ruby, Python, etc.

https://mobile.twitter.com/woketopus/status/1447150924846313...

Reply


@pdenton 10 hours

Replying to @rehhouari 🎙

Having used PHP since version 4 (20 years ago), I can say with confidence that its had its peak. Everything new added since 7.4 is just fluff or bloat. Fixing long standing bugs and performance improvements is fine, of course. But everything new they introduce adds to the pile of warts that's PHP. I highly regard those that try to improve it, but with the current direction it'll remain the colloquial example of how not to design a programming language.

Reply


@superasn 13 hours

Replying to @rehhouari 🎙

I really wish Php added support for Perl's qq and q operators sometime too (1)

It would really make writing scripts a lot easier and cleaner.

(1) https://stackoverflow.com/a/41334570/1031454

Reply


@hollosi 5 hours

Replying to @rehhouari 🎙

PHP's main problem is its very shallow learning curve.

The 5840 built-in functions provide easy ways to get many useful things done with minimal dabbling.

In other words, one can be a "scripter" instead of a "programmer", and for many projects that's 100% OK.

Other languages automatically filter out "scripters" from their field by demanding a certain level of mental abstraction to produce anything moderately useful.

While there is absolutely no difference between a good programmer using PHP, or a good programmer using any other language/environment, but there is a big difference between the median levels and below.

This in turn lowers the reputation of the field, and it becomes uncool to put PHP on one's resume, therefore the more capable programmers propagate out to other languages, which further lowers the median, etc.

This happened with Perl, too...

Reply


@fevangelou 6 hours

Replying to @rehhouari 🎙

I remember inheriting a RoR v2 based project. Ended up virtualizing the entire server to migrate the damn thing as it was a big pile of dependency mess to port to a newer server stack. On a similar situation with a PHP 4 based codebase, adapting it to work on PHP 5.6 was a few hours work. Done. No other language can beat that. And thanks to a newer (and more open minded) generation of developers, PHP has a long way to go. The launch of the PHP Foundation by Jetbrains, Automattic and others - https://blog.jetbrains.com/phpstorm/2021/11/the-php-foundati... - is also a step towards a better future for PHP.

Reply


@dekerta 9 hours

Replying to @rehhouari 🎙

I've never understood why some programmers like to use ligatures, especially on a cheat sheet like this, which will be used primarily by people who aren't familiar with the syntax.

It's not 'a' ⇒ 1, it's 'a' => 1, just write it out the correct way. Why are you trying to be fancy?

Reply


@igammarays 12 hours

Replying to @rehhouari 🎙

As a solo bootstrapped founder working on PHP (Laravel) every day, I was surprised to learn a LOT from this one page. Thanks for posting.

Reply


About Us

site design / logo © 2021 Box Piper