Live data from Hacker News

Modern PHP Cheat Sheet

front-line-php.com

91–100 of 127 posts

Re: Modern PHP Cheat Sheet

#91

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…

Is it really useful to have ordered map as the "only" collection type?

I guess it might be Stockholm syndrome - but I'm quite comfortable with a "list/array" type (numeric keys, ordered) and a "map/hash_map" type (objects as keys).

Especially give that with phps "power" you can:

  $array = array(
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
  );
  var_dump($array);
Ouputs:

  array(1) {
    [1]=>
    string(1) "d"
  }
https://www.php.net/manual/en/language.types.array.php

Re: Modern PHP Cheat Sheet

#92
post #78

Earlier quoted context omitted.

My personal favorite PHP wtf is this one: if (md5('240610708') == md5('QNKCDZO')) { echo "true\n"; } Though I do use php, and love many things about it.

Do you know why that is? php > echo md5('240610708'); 0e462097431906509019562988736854 php > echo md5('QNKCDZO'); 0e830400451993494058024219903391

Here is the explanation: https://www.php.net/manual/en/function.md5.php#123392

Re: Modern PHP Cheat Sheet

#93
post #86
post #78

Earlier quoted context omitted.

My personal favorite PHP wtf is this one: if (md5('240610708') == md5('QNKCDZO')) { echo "true\n"; } Though I do use php, and love many things about it.

Having been out of PHP for several years now, I assume that needs a === instead of ==? It's unfortunate that we have the need for a ===, but it's no different that JS in that regard.

The underlying issue is that while md5() thinks it is returning a hex number, if the hex number starts with 0e, php thinks it's 0^.

Re: Modern PHP Cheat Sheet

#94

Earlier quoted context omitted.

> People seem to love to hate it because it's the "cool" thing to do. While I agree with this, I think the tone is slightly dismissive. From what I have seen, older people in our trade seem to recommend against PHP due to it having a terrible relationship with consistency, functionality, performance, etc. in its history. Though I have (thankfully) seen that the language and its standard library has undergone several…

> From what I have seen, older people in our trade seem to recommend against PHP due to it having a terrible relationship with consistency, functionality, performance, As an older person in the trade, I don't agree with the other older persons. Only consistency is a problem with PHP code, and that is largely NOT caused by PHP. Inconsistency is the product of developers coding without any real thought about how to abs…

I am honestly and truly trying my absolute best, but no matter how hard I try, I again and again completely fail to sufficiently and comprehensively put into meaningful words how the inconsistency of the PHP language doesn’t play a role AT ALL when building actual web applications for actual users.

It just. Doesn’t. Matter. Choose Symfony or Laravel, and you are in for a wonderful development experience, from setup to dependency management to architecture to implementation to testing to deployment – for certain kinds of projects it’s just perfect.

Re: Modern PHP Cheat Sheet

#95

What is modern PHP?

It commonly refers to PHP 7+ paired with clean code practices and SOLID principles. The opposite is PHP5-style code from the early 2000s, no package management, no type strictness, no object orientation, just a giant spaghetti mix of JS, HTML, SQL and PHP in a 10kloc file.

Do you think spaghetti code won't come from folks who use Composer? Ha!

Re: Modern PHP Cheat Sheet

#96

Earlier quoted context omitted.

Ok say I have a small class with an options object (I know perhaps not the best design...): class Foo { public function __construct(string $name, array $options) { $this->name = $name; if ($options->enableFlag) { ... I want to declare what $options are, in TS I could do: type TFooOptions = { enableFlag: boolean; userIds: number[]; } Then function __construct(string $name, TFooOptions $options) ... Otherwise how do I…

>type TFooOptions is that creating a JS class under the hood or is just a hint for the compiler? What I do , but I don't work with recent PHP versions is to create this as a class, then to make this easy to construct objects from JSON i add a static function fromObject or fromJson that does it for me. Using real classes could prevent bugs when working with shit APIs like Microsoft TTS , where they have 2 endponts and…

Simple object type in TypeScript, the "type checking" abstraction layer on top of JS, which actually doesn't even need to be compiled (eg. esbuild strips all typing from the code before compiling).

https://www.typescriptlang.org/docs/handbook/2/everyday-type...

Your approach is sensible. I remember using json_encode() indeed so I could declare some data in a php file with <<< HEREDOC in the Javascript short syntax (though short array syntax in php nowadays makes it less painful, JSON syntax still a bit less verbose).

Re: Modern PHP Cheat Sheet

#97
post #32

Earlier quoted context omitted.

It's so nice to see a comment giving PHP the praise it deserves. Honestly, PHP is a good, if not great, language to use for building solid web apps fast. People seem to love to hate it because it's the "cool" thing to do. Yet PHP developers continue to ship things faster, while JS devs are probably still fiddling with their Node environment setup...

> People seem to love to hate it because it's the "cool" thing to do. While I agree with this, I think the tone is slightly dismissive. From what I have seen, older people in our trade seem to recommend against PHP due to it having a terrible relationship with consistency, functionality, performance, etc. in its history. Though I have (thankfully) seen that the language and its standard library has undergone several…

> It's why we get things like Deno, which attempt to throw all this cruft away and start again

Doubt. Your "something@v1.2.3" in Deno is no different than "something": "^1.2.3" in npm. The problems will appear when you update stuff.

The only thing benefiting deno currently is the clean slate and the lack of hacks (for now).

Re: Modern PHP Cheat Sheet

#98
post #78

Earlier quoted context omitted.

My personal favorite PHP wtf is this one: if (md5('240610708') == md5('QNKCDZO')) { echo "true\n"; } Though I do use php, and love many things about it.

Do you know why that is? php > echo md5('240610708'); 0e462097431906509019562988736854 php > echo md5('QNKCDZO'); 0e830400451993494058024219903391

Would this be avoided by using a string comparison function?

https://www.php.net/manual/en/function.strcmp.php

Re: Modern PHP Cheat Sheet

#99
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.

Re: Modern PHP Cheat Sheet

#100

Earlier quoted context omitted.

I agree. Developer speed does not get enough coverage when comparing languages. When I work with a Node project it's such a slow dragging build process. Using PHP is like a fun superpower that lets me turn out things on timescales that are borderline miraculous to clients and management.

Hiring developers is rarely considered as well. I can hire a top-notch PHP engineer for half of what a mid-level JS engineer would be asking. It's not that PHP engineers are less qualified than JS. I think bootcamps shifted the market for JS developers and now really good engineers cost an arm and a leg while good junior engineers start around the $100k mark. The whole JS salary market is insane and as a startup, I c…

I think it’s interesting to me, as I see these comments a lot with PHP specifically and there is at any honest pushback on this, so here’s my attempt:

Just pay what good engineers should be paid. If there are PHP developers out there not getting the same salary as a their peers like this post mentions, you should leave that job for a better one. Please demand better, PHP developers, for all of our sakes. Business always wants to put downward pressure on salary and we need to be united against this regardless of preferred tech stacks

Post reply on HN