5 ms·
Conversely, whenever I see people talking about server-side JS, I can't find any reason why I wouldn't use PHP instead. PHP has a vastly simpler toolchain (mak
by Gormo 6mo ago
Conversely, whenever I see people talking about server-side JS, I can't find any reason why I wouldn't use PHP instead.
PHP has a vastly simpler toolchain (making it much more effective for rapid iteration), much more consistent and well-thought-out syntax, a more extensive standard library, type safety without having to transpile code from another language (so no build processes that rival C++ in complexity just to still have interpreted code at the end), native and full-featured object orientation, generally better runtime performance, and a package ecosystem with Composer that isn't overrun with inane vanity projects and supply-chain vulnerabilities.
The only major downside to PHP is that it's not great at multithreading, but if you're building microservices where parallelization is handled by an external orchestrator, then you can design around that pretty effectively.
- Tadpole9181 6mo agoAs someone who prefers PHP in general and find the TC39 committee has kneecapped the JS language in the past few years... > PHP has a vastly simpler toolchain Firmly disagree. You can install Node and have a basic server running in a few seconds. PHP requires installing and setting up a server tied into FPM and then reconfiguring a slurry of bad defaults. If you don't avoid the footgun of "traditional" deployments, you get to deal with mixed versions of source. If you don't avoid the footgun of "native" sessions, you get to deal with INCOMPLETE_CLASS errors galore. And if you want a dynamic frontend, you're still going to want to bust out JS. > I can't find any reason why I wouldn't use PHP instead Using a single language for both frontend and backend with (largely) the same availability of tooling and the ability to share code (i.e. type definitions). > generally better runtime performance I find this hard to believe? Intuitively, I would assume that the Node / Bun engines are significantly faster than PHP - which doesn't even come with it's JIT enabled by default on the (perfectly valid) note that backends are almost always constrained by DB times. > a package ecosystem with Composer that isn't overrun with inane vanity projects and supply-chain vulnerabilities. Functionally, Composer is worse than any of the NPM package managers IMO. PHP's global, shared namespaces preventing monkey patching and nested dependencies is a huge burden when you need to use Lib A and Lib B, but both have conflicting dependencies on Lib C. But the only reason it doesn't suffer (as many) supply chain issues is two-fold: 1. Packagist's source of truth is the repo and tags. It's much easier to notice a Github account being compromised, which is already harder because it's always had better account security expectations, than NPM. But this comes at costs - such as being unable to rename a package in-place, because Composer gets really confused when it clones a trunk that has a different name than what you asked for. And it's not intrinsically more secure, since tags are not immutable and people can host on less secure VCS platforms. 2. But more than that... it's just less used? The PHP ecosystem is noticeably smaller and has way less happening. So its very much trade-offs.
- runjake 6mo ago> You can install Node and have a basic server running in a few seconds. PHP requires installing and setting up a server tied into FPM... Without mentioning more, the PHP equivalent to your Node example is `php -S`.
- chuckadams 6mo agoOr FrankenPHP, or hell, there's still even good old Apache. Or avoid the SAPI interface entirely with servers in PHP like Workerman, AMPHP, or Swoole. FPM is entirely too fussy for me to bother with: its error handling is atrocious (restarting in an infinite loop with no backoff is common), and no one really knows how to tune it.
- Gormo 6mo agoThose are great solutions for production deployment, but to the previous commenter's point, for iterating on your local machine during development work, nothing beats just running `php -S`. Launch the interpreter's built-in dev server in your project directory, load up localhost in your browser, work on your code, and testing incremental changes locally is just a matter of hitting F5.
- moebrowne 6mo agoBy default the dev server is single threaded, but since PHP 7.4 you can add more with an env: `PHP_CLI_SERVER_WORKERS`
- archerx 6mo agoI’ve been using PHP for over a decade and have never used FPM. “Using a single language for both frontend and backend with (largely) the same availability of tooling and the ability to share code” Is a negative I went backend world and front end world to be different because they do very very different things. “But more than that... it's just less used? The PHP ecosystem is noticeably smaller and has way less happening.” That’s not true, PHP is less resume driven development and actually about productivity. I’m really happy narcissist hate PHP and don’t burden it with their garbage and slopworks.
- dgb23 6mo agoPHP's performance can be significantly lower than JS, because it doesn't have application state (in a standard runtime/setup) and needs to re-run the entire application for every request. Now there are a whole bunch of tricks both in the language and with tooling to alleviate that, but still it's inherently there. It's an advantage for other reasons though.
- g8oz 6mo agoI agree that PHP's request oriented "shared nothing" approach has its advantages. That being said there are very decent options for long running processes/application servers these days - see RoadRunner, Swoole and Frankenphp.
- Meekro 6mo agoThere are advantages to the lack of application state, though. Memory leaks and similar bugs became largely irrelevant, for instance. Regarding performance, a simple LAMP stack on a dedicated machine can easily give you <250ms pageloads for many web apps. If that's not fast enough, or you're averaging dozens or hundreds of requests per second, you're probably big enough that you can use parallelization or more exotic architectures to speed things up.
- NorwegianDude 6mo agoA simple LAMP stack can give you <1ms page loads for most applications. Even 15 years ago you could serve over 100k/rps of lightweight PHP pages from a low end server.
- troupo 6mo ago> because it doesn't have application state (in a standard runtime/setup) and needs to re-run the entire application for every request. Where "application" is basically a single page with less code than a typical React page. Even 20 years ago you'd run into DB struggling to give you data fast enough before you hit any issues with the "re-running the entire app". And you have to screw your database really badly to see any issues early. Hell, phpBB was horrendously bad, running dozens of heavy DB queries on each page, and was still powering some of the internet's busiest forums. > Now there are a whole bunch of tricks both in the language and with tooling to alleviate that, but still it's inherently there. It's an advantage for other reasons though. Yes. It is an enormous advantage: it's fire and forget. You don't need to "SSR" your app (getting all data and state), ship it to the client with a bundle, then "re-hydrate" it (once again pulling data and state from the server) etc.
- Meekro 6mo agoIt's true that the lack of multithreading in PHP has been a persistent pain. I love PHP and I've done PHP-centric projects for 20 years, but I end up falling back to Go when it's time to write a background worker that will handle lots of tasks in parallel. I really wish my PHP apps could include native components that process background tasks in parallel. On the other hand, Javascript's parallelization is one of the hardest-to-understand things I've ever seen: in order to really grasp the async/await model, you have to know the whole story of callback hell and how modern Javascript papers over it while carefully preserving backwards compatibility.
- sidkshatriya 6mo ago> It's true that the lack of multithreading in PHP has been a persistent pain. No actually it's a joy to have no multithreading. It keeps the complexity budget lower. You can do multithreading through a custom PHP module if you had a very specific need. Maybe my requirements were too simple but I've never really felt the need. The shared nothing PHP architecture really helps you get away with this. Anyways as the parent comment said: > but if you're building microservices where parallelization is handled by an external orchestrator, then you can design around that pretty effectively.
- martinald 6mo agoI feel like I'm on a different planet when I see this kind of comment. What if you need to call multiple external APIs at once with complex json? Sure you can call them one after another, but if each take (say) 2s to return (not uncommon IME), then you are in real trouble with only one thread - even if it is just for one "request". I guess I'm spoilt in .NET with Task.WhenAll which makes it trivial to do this kind of stuff.
- theodorejb 6mo agoThis can be done with curl_multi_exec(), or with $client->getAsync() in Guzzle. https://www.php.net/manual/en/function.curl-multi-exec.php https://www.php.net/manual/en/function.curl-multi-exec.php https://docs.guzzlephp.org/en/stable/quickstart.html#concurrent-requests https://docs.guzzlephp.org/en/stable/quickstart.html#concurr...
- bakkoting 6mo ago> type safety without having to transpile code from another language (so no build processes that rival C++ in complexity just to still have interpreted code at the end) All major server-side JS runtimes are capable of executing TypeScript without transpilation these days. Complex build processes are only really a thing for client-side JS. The normal state of affairs these days is to run server-side code with no build step whatsoever.
- Gormo 6mo agoThat's true to some extent, but working with many modern frameworks often still requires a build step. Next.js in particular has quite a heavy toolchain.
- sellmesoap 6mo agoI think frankenphp checks a lot of boxes here, go to handle the threads with php built in https://frankenphp.dev/ https://frankenphp.dev/