Live data from Hacker News

PHP is worth learning and using

bulletproofphp.dev

381–390 of 468 posts

Re: PHP is worth learning and using

#381
post #352

I really don't understand all the PHP dismissal on HN. As a language it has continued to evolve in meaningful ways, especially with the latest releases. Companies like Facebook are built on PHP (yes, they extended with Hack, but most of the benefits are available in PHP 8 now without the downsides). Coming from a C background, I found PHP easy to step into as many of of the underlying libraries and common functions a…

I work mostly in PHP for my day job, and honestly even I can understand people being dismissive of PHP. It undeniably has many advantages you and others mentioned, but equally undeniably it was from the very beginning a huge kludge (famously, its creator only intended today's "PHP language" as a template language, and when its scope gradually grew to a "full-fledged" language, nobody really cared about consistency). And this is a legacy which they have since tried their best to shake off, but they will of course never be able to get rid of it completely.

Also, same as many other open source languages, there seems to be a tendency to pick up every language extension someone proposes hoping that it might win over some developers, but I'm not really convinced. Does PHP really need a "match" control structure (https://www.php.net/manual/en/control-structures.match.php) when it already has good ol' "switch" and "if/else"? Of course, I also like the more flexible (Pascal/)Go-style switch statement better than the "classic" C-style one, but maybe they should have given it some more thought years ago instead of slavishly following C/C++ then and inconveniencing thousands of developers by introducing a new keyword now?

Re: PHP is worth learning and using

#382

Earlier quoted context omitted.

Have you used a version of php from the past decade? Or are you conflating a bad framework or codebase with a bad language?

Yes, it's like an old Model T Ford from the early 1900s that has had a turbocharger, air conditioning, a new stereo, a subwoofer, roof rack for a bike, roof rack for a tent, grounding straps, trailer hookup, clean water system, etc.. bolted onto it. At some point you ask yourself - why did I do all of this to a Model T? Sure, it has some modern facilities but the language itself was never designed and so none of it f…

You have to give up arguing.

The PHP dev will look at a Model T DIY'd into "The Homer", and beside it a 2016 Toyota Corolla.

The PHP dev will argue, sincerely, that The Homer is just as practical of a daily driver as the Corolla. You have to just walk away from this kind of person.

Re: PHP is worth learning and using

#383

Hmmm... Most points in this article are shouting "yeah we have those too!!" but it didn't give any convincing arguments why I (or anybody who doesn't already know PHP) would like to learn and use PHP over other decent modern languages and ecosystems, which happen to have those points as well.

My favorite thing about PHP is the execution model. Every single request is brand new and runs the whole program start to finish. IMO that's extremely powerful in its simplicity and while you can get it in other languages, it feels like like a "core competency" for PHP (just like how async code is easier in javascript than in python, because JS kind of had it from day 1)

Async code is a good way to achieve some level for performance in I/O bound workloads, but it's a pain for the programmer. Because of that I wouldn't say that it's necessarily a good thing that JS is async-first.

In my opinion, high level languages should not expose async primitives to the programmer - this should be abstracted away by the language and its runtime. Go is doing this (all I/O is async under the hood), and it's awesome: You can write synchronous/sequential code and get the performances of async. I would love if other languages copied that.

Re: PHP is worth learning and using

#384

Hmmm... Most points in this article are shouting "yeah we have those too!!" but it didn't give any convincing arguments why I (or anybody who doesn't already know PHP) would like to learn and use PHP over other decent modern languages and ecosystems, which happen to have those points as well.

Frankly my reasons for using it boil down to: 1. WordPress uses it 2. PHP is far less terrible than I remember (having used it in very early days) I manage a WP (woocommerce) site for a small business. It's not my day job, it needs to be simple. Most of the "day job" is handled by content management users. I have written a fair amount of plugins though to enable custom functionality and the process is perfectly fine.…

I would not recommend anyone to use WordPress in 2021, though. WordPress maybe simple to setup, it's part of what gave PHP its bad reputation.

Re: PHP is worth learning and using

#385

I have used PHP for years. Like it. Have accomplished some nice things with it. Think it's evolved nicely in recent years. Feel it's a great tool for particular problems. Will continue to use it. Will always be open to alternatives. Will never understand why some people find that so offensive and will tell you that you mustn't use it or imply that you are ignorant or stupid for using it.

You’re writing style is unique and grabbed my attention. Any tips to write like that? You didn’t use “I” at all, but it seems likely there are some other rules going on here.

I was just trying to get a number of key points across calmly and succinctly without wasting time diving into a long-winded academic debate about whether or not I am "allowed" to use a programming language that I have used with success for the last decade!

Not sure that I have any tips for you. Other than that it is often helpful to remove unnecessary words when communicating. This is something I have become sensitive to over the years. I used to write a lot more voluminously but I find it is harder to convey messages to people when they are confronted with a wall of text.

Re: PHP is worth learning and using

#386
> Early PHP was not intended to be a new programming language, and grew organically, with Lerdorf noting in retrospect: "I don't know how to stop it, there was never any intent to write a programming language [...] I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way." [0]

This might be an unpopular opinion but I think PHP gained mindshare in the early days because Coldfusion (CFML) was neither free nor open source.

[0] https://en.wikipedia.org/wiki/PHP

Re: PHP is worth learning and using

#387
post #336

I'll be honest. I really don't like PHP. However when I was running a coding workshop, very frequently people would ask: "How do I make a web page talk to a database?" I'd say: Well, you know that HTML file? Rename it to become a PHP file and you're almost there! As pretty much all cheap hosting already offers support for PHP (and MySQL for that matter) this would almost always work. I would love to be able to do thi…

You hit the nail on the head, this is exactly why I fell in love with PHP. I learnt it back in the PHP4 days, and it just seemed like the perfectly straightforward way to do web. In a similar fashion, I abstracted away the database connection, query and returning of results into one lazy-inited function, so talking to the database really was just like I worked a lot with other languages, none feeling as natural to me…

Be careful as this syntax can potentially introduce SQL injections.

PHP's parameterization features in PDO can be abstracted so you can turn this into:

  $vars = array(":userid" => $userid);
  q("select name from users where id = :userid", $vars);
It's still pretty concise and is much safer.

Re: PHP is worth learning and using

#388
post #54

Earlier quoted context omitted.

I think there's value in a large, established ecosystem. Languages tend to go through a few years of flavour-of-the-week fads (think of the Vue/React/Angular competition, and all the also-rans that we can't remember now). If you get in too early, there's a fair chance you'll bet on the wrong ecosystem, and end up with unsupported dependencies or difficult to maintain code. In the worst case, you scrap the whole thing…

jQuery changed my life. These days I write server code in C++ :(

Why so sad, that sounds like a huge improvement over webdev. Modern C++ is a joy to work with.

Re: PHP is worth learning and using

#389
post #56

Earlier quoted context omitted.

> Is the "your app gets initialized and torn down for every request" true for frameworks like Laravel and Symfony? Yes, though heavy optimization in opcode caching and JIT keeps it fast and the primary process manager today (php-fpm) generally keeps a pool of PHP processes persistent. Each request is initialized from scratch but there's no disk nor interpreter hit after the first. There are, of course, frameworks tha…

I don't agree with Symfony being inherently "very heavy". As set of framework components, you can any part you find useful, and wire to together yourself, if necessary. e.g. https://symfony.com/doc/current/configuration/micro_kernel_t... Many other PHP projects, such as Laravel and Drupal pick-and-choose Symfony framework components in this way.

Sorry, I should have clarified "heavy" in terms of the level of OOP [1] insanity but yeah Symfony is very modular.

[1] e.g. "Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator"

Re: PHP is worth learning and using

#390
post #387

Earlier quoted context omitted.

You hit the nail on the head, this is exactly why I fell in love with PHP. I learnt it back in the PHP4 days, and it just seemed like the perfectly straightforward way to do web. In a similar fashion, I abstracted away the database connection, query and returning of results into one lazy-inited function, so talking to the database really was just like I worked a lot with other languages, none feeling as natural to me…

Be careful as this syntax can potentially introduce SQL injections. PHP's parameterization features in PDO can be abstracted so you can turn this into: $vars = array(":userid" => $userid); q("select name from users where id = :userid", $vars); It's still pretty concise and is much safer.

Two further alternatives for improved expressiveness:

  q('select name from users where id = :userid', compact('userid'));

  q('select name from users where id = ?', [ $userid ]);
Recommend using single quotes for SQL (command) literals, rather than doublequotes. This helps with discouraging string interpolation (" ... WHERE col = $value "). This also helps very much with SQL quoting object names (tables, columns, indexes, etc) - SQL specifies doublequote (") as the quoting character; for example 'SELECT COUNT(users.id) AS "Number of users" FROM users', or 'CREATE VIEW "My daily report" AS SELECT SUM("count") FROM "some strange table" LEFT JOIN ...'.
Post reply on HN