PHP is a language with various features that might make it attractive to certain people:
• CGI-like execution model when used for the web, making resource leaks very difficult and encouraging scalable design
• not only integers and floats, but also strings, lists and dictionaries as mutable value types (copy-on-write) — this is a big difference from JavaScript and Python
• unusually for a dynamic “scripting” language: true classes and interfaces, and optional type declarations enforced by the runtime
• a standard library that doesn't require import statements!
• (a very new feature:) fibers, a way do asynchronous execution without every function in the callstack having to be aware of it
• binary-safe strings without presumptions from the language about what encoding is in use
• a very nice package manager
• a highly optimised runtime
But its history has made it a quite messy language which can be frustrating to learn, and which will always carry a higher mental burden than something like Python. It is a shame.
I don't think fibers are that great. There's no indication that a function call will create a fiber and do things[1] that might screw with your current state. When you know fibers are around, you need to code more defensively.
> not only integers and floats, but also strings, lists and dictionaries as mutable value types (copy-on-write)
Lists and dictionaries are also mutable in Python. Strings are not. With CoW, are your referring to garbage collection? (value that is no longer assigned to a name will eventually be purged). That would be independent of (im)mutability, so I'm not sure I understand.
> a standard library that doesn't require import statements
I consider this a non-feature in JS. The global namespace is enormous and growing rapidly. As I recall, PHP’s global namespace is similarly enormous (and notorious for its inconsistencies and redundancies). I understand that there are ergonomic advantages to not having to import built-ins, but I think framing it as a feature deserves a healthy asterisk :)
> fibers, a way do asynchronous execution without every function in the callstack having to be aware of it
This isn’t built into JS, but there’s an implementation of it, mostly used and popularized by Meteor. Again it has some ergonomic benefits, but I think it can too easily hide/obscure deoptimizations—eg performing IO operations in sequence which would be better performed concurrently.
It's a decent website - a useful collection of information and links to additional information, that can serve as a starting point to learn more. For those who use PHP, or for those who'd like to learn to use it, this might be useful resource.
The same thing can't really be said of the comments in here though. I don't see how emotionally charged debates over what is the "best" or "worst" language, help anyone.
It's almost as if some people can truly have their ego shattered, by someone on the internet liking another language than they do...
Couldn't we just agree that there is no "best" language for every possible use case, and that everyone is allowed to like whatever language they want to like? It's not like the whole bickering is going to change anyone's opinion.
In its early days (version 4 and below) PHP was a bit of a mess. However it got its act together with version 5, as it implemented an object model almost on par with Java's, which I thought kind of set the standard for how OOP should be done. It's just unfortunate that much of the mistakes with version 4 and below had to be kept in order to preserve backwards compatibility.
You're right, but you're missing the emotional damage many hackers of a certain age feel when they remember troubleshooting PHP problems, or constantly having to look things up, before they left for whatever their language of choice is now. People complaining about PHP are mostly just venting frustrations from 15 years ago.
A language that changes the semantics of functions between minor versions has no "right way".
"count()"less examples of crap. I have currently two weeks of code migration behind me because code running correctly on 7.0 fails on 7.3 -- while I see the reason for change was necessary I also see the hopelessness of rebuilding a flawed house with a fundament build on a pile of manure.
And the good news given to me by a colleague is that they pulled the same stunt going from 7.3 to 7.4. It's like depending on stumbling alcoholics instead of language designers.
Not much has changed since core devs tried to mitigate an overflow by testing for "$variable < MAXINT".
1. Core API of stateless functions. Compare this to the bootstrapping requirements of, say, Java or Python;
2. CGI-like resource management in that you tear down everything after a request. Many people get upset at, say, the "global" keyword but "global" here just means "request-scoped". There are no STW GC pauses and it's difficult (but not impossible) to leak resources;
3. Executing code for a request is single-threaded. This is almost always what you want; and
4. PHP's copy-on-write arrays (that are really hash maps) that maintain insertion order are so incredibly useful and is almost always what you want.
Crapping on PHP for things like function inconsistency (eg functions that use underscores vs those that don't, needle/haystack inconsistency) is a tired cliche and boring at this point. It really adds no value complaining about things that just don't matter and/or are historical.
My biggest grievance is that Hack (FB's fork) is basically better in almost every way. The collection types (vec, keyset, dict) are better (eg less weird coercion defaults). The type system is leaps and bounds ahead of anything PHP has. Nullability as part of the type system is amazing. It's a shame Hack didn't become the dominant dialect.
As for the book, I went through it and it's really solid. Good job to the authors and maintainers. It covers a lot of issues like register globals, sanitizing input, Unicode and so on. It also has links to more detailed information on any of these topics.
Actually, that make me remember statements of Linus Torvalds on Git at Google Tech Talk Conference:
But I did end up using CVS for seven years at a commercial company and I hated it with a passion. When I say I hate CVS with a passion, I have to also say that if there are any SVN users in Subversion, users in the audience, you might want to leave because my hatred of CVS has meant that I see Subversion as being the most pointless project ever started, because the slogan for Subversion for a while was, CVS done right or something like that.
And if you start with that kind of slogan, there’s nowhere you can go. It’s like — there is no way to do CVS right. So that’s the negative kind of credit.
> But, really, I suggest using other tools. We have nicer tools now.
Debatable really. Within the web development space the options are rather limited in my opinion.
Options:
* Spring based apps - Downside JVM is a Resource hog
* Python, Ruby, etc - The web frameworks are in my opinion not as full featured. Same with CMS systems. Also requires more resources. Also have limited OO features.
* Go, Rust, etc - Really good choices for APIs but their web frameworks are also really lacking.
The tooling around PHP is really good and stable. The language itself, the downsides are very limited and the main one that remains valid is inconsistency of function parameters. The issues with bad code are generally the same ones that can be done in another langauge.
As someone who had to learn a bit of PHP recently, I really don't see why would anyone pick it up these days other than vendor/business lock in. Laravel is great but there are hundreds of equally as great frameworks on dozens of more moderns and better though out languages: go, python, nim, ruby etc. A bunch of useful functions expanded into the default namespace is no longer that special.
One that baffles me is that 8.0 changed it so all scripts default to a "C" (i.e. english, ascii-only) locale. [0]
So this is now a scripting language that requires you to call `setlocale` for every script manually.
Contrast python, which tries to fix the locale so it's at least UTF-8 aware. [1]
[0]: From https://www.php.net/manual/en/migration80.incompatible.php "The default locale on startup is now always "C". No locales are inherited from the environment by default. Previously, LC_ALL was set to "C", while LC_CTYPE was inherited from the environment. However, some functions did not respect the inherited locale without an explicit setlocale() call. An explicit setlocale() call is now always required if a locale component should be changed from the default. "
The beauty of perl scripts, and later php templates, was in their BASIC-like simplicity. They were not secure or structured in any way, but very accessible.
Once you add these frameworks, you realize you could have started with Python or Java in the first place and get more mature infrastructure and better language along the way.
> Once you add these frameworks, you realize you could have started with Python or Java in the first place and get more mature infrastructure and better language along the way.
And, with things like Flask or FastAPI, you get BASIC-like simplicity and immediacy with a language that has a concept-space that scales with your understanding of it.
I have used PHP as a replacement for even shell scripts, like perl and python, a lot in the past. The best thing is the very strong standard lib. Everything's built-in. I like node as well, but having to install so many packages just for that one file script is a major annoyance.
> Right now PHP does not support Unicode at a low level. There are ways to ensure that UTF-8 strings are processed OK, but it’s not easy, and it requires digging in to almost all levels of the web app, from HTML to SQL to PHP. We’ll aim for a brief, practical summary.
That sure is a big a web page for having no table of contents!
There’s a ToC on https://leanpub.com/phptherightway but I can’t link directly to it as the in-page link uses JS. (I’m on mobile though, so I can’t read the source. Maybe there’s an anchor someone else could link to.)
To answer the immediate question, I had a look and nooope, no <a name="">, not even <a> anywhere, that I could find. However I just (begrudgingly) realized this is somewhere text fragments can shine: https://leanpub.com/phptherightway#:~:text=Getting%20Started
To answer the bigger-picture question, the first thing I see (the above-the-fold part) upon loading the page (on both desktop and mobile) is a stylized ToC.
I'm not sure what you're seeing, but for me the page starts with a massive ToC. On even smaller screens it turns into a button fixed at the bottom of the screen that opens a ToC in an overlay.
I want to like PHP, but it seems that there are so many better/modern alternatives out there, IMHO Go and Rust are the most notable new ones, Go is almost (if not yet) ahead of PHP in terms of community and resources (learning and packages), while Rust still needs a few years to establish (the async issue and few others) and the maturity of the ecosystem, there are no killer solution yet (maybe there will never be).
I tried PHP last year (or should I say Laravel) and didn't found any good, the type system is bad for a project like Laravel, you have too many different files where you declare things and the type system doesn't now, so you end up without autocompletions (and warnings or errors from the php interpreter) you only catch those at runtime (I'm too used now to get the errors before run a project, or at least get a hind from the editor/IDE).
Not sure about your experience with Laravel, but I don’t see how the type system is bad for it. It works surprisingly well for me. With a bit of tooling I get even autocompletions for database fields when using Eloquent. PhpStorm with the (paid) Laravel plug-in are pretty awesome and give me more information during file-editing than for example Xcode.
Laravel has also a pretty active and productive community and so many things just work out of the box, I’m not sure if Go or Rust has anything comparable.
I have to mention: I didn’t touch Go for over a year now and never used Rust. I felt that Go is pretty easy to learn in its basics but quickly throwing things together for a prototype with DB access, I would go for PHP at any time.
Slightly of topic, but is Hack a thing outside of Facebook? I'm a little thrown off by the confusing standard library of PHP, but I don't know if using Hack instead would solve more problems than it creates.
Updated in January but not really up to date. It doesn't mention union types, the nullsafe operator, matching expressions and a lot of other features you can find here: https://stitcher.io/blog/new-in-php-8
Maybe not everything is "the right way" but at least the nullsafe operator is, I think.
I'm trying to learn https://haxe.org , so that if some target language does not work, I could use some other target language. Currently I have hard time updating to newer version of Node.js, dependencies etc. I tried to code something with PHP 8.1 directly, but I presume any following update could change syntax etc.
[+] [-] TazeTSchnitzel|4 years ago|reply
• CGI-like execution model when used for the web, making resource leaks very difficult and encouraging scalable design
• not only integers and floats, but also strings, lists and dictionaries as mutable value types (copy-on-write) — this is a big difference from JavaScript and Python
• unusually for a dynamic “scripting” language: true classes and interfaces, and optional type declarations enforced by the runtime
• a standard library that doesn't require import statements!
• (a very new feature:) fibers, a way do asynchronous execution without every function in the callstack having to be aware of it
• binary-safe strings without presumptions from the language about what encoding is in use
• a very nice package manager
• a highly optimised runtime
But its history has made it a quite messy language which can be frustrating to learn, and which will always carry a higher mental burden than something like Python. It is a shame.
[+] [-] babuskov|4 years ago|reply
It's the main reason why I still prefer PHP for quick&dirty scripting and data processing.
[+] [-] withinboredom|4 years ago|reply
[1]: https://withinboredom.info/blog/2022/01/04/thoughts-on-php-f...
[+] [-] diarrhea|4 years ago|reply
Lists and dictionaries are also mutable in Python. Strings are not. With CoW, are your referring to garbage collection? (value that is no longer assigned to a name will eventually be purged). That would be independent of (im)mutability, so I'm not sure I understand.
[+] [-] eyelidlessness|4 years ago|reply
I consider this a non-feature in JS. The global namespace is enormous and growing rapidly. As I recall, PHP’s global namespace is similarly enormous (and notorious for its inconsistencies and redundancies). I understand that there are ergonomic advantages to not having to import built-ins, but I think framing it as a feature deserves a healthy asterisk :)
> fibers, a way do asynchronous execution without every function in the callstack having to be aware of it
This isn’t built into JS, but there’s an implementation of it, mostly used and popularized by Meteor. Again it has some ergonomic benefits, but I think it can too easily hide/obscure deoptimizations—eg performing IO operations in sequence which would be better performed concurrently.
[+] [-] hooby|4 years ago|reply
The same thing can't really be said of the comments in here though. I don't see how emotionally charged debates over what is the "best" or "worst" language, help anyone.
It's almost as if some people can truly have their ego shattered, by someone on the internet liking another language than they do...
Couldn't we just agree that there is no "best" language for every possible use case, and that everyone is allowed to like whatever language they want to like? It's not like the whole bickering is going to change anyone's opinion.
[+] [-] pak9rabid|4 years ago|reply
[+] [-] dec0dedab0de|4 years ago|reply
[+] [-] unknown|4 years ago|reply
[deleted]
[+] [-] benjamir|4 years ago|reply
And the good news given to me by a colleague is that they pulled the same stunt going from 7.3 to 7.4. It's like depending on stumbling alcoholics instead of language designers.
Not much has changed since core devs tried to mitigate an overflow by testing for "$variable < MAXINT".
[+] [-] jonwinstanley|4 years ago|reply
[+] [-] cletus|4 years ago|reply
1. Core API of stateless functions. Compare this to the bootstrapping requirements of, say, Java or Python;
2. CGI-like resource management in that you tear down everything after a request. Many people get upset at, say, the "global" keyword but "global" here just means "request-scoped". There are no STW GC pauses and it's difficult (but not impossible) to leak resources;
3. Executing code for a request is single-threaded. This is almost always what you want; and
4. PHP's copy-on-write arrays (that are really hash maps) that maintain insertion order are so incredibly useful and is almost always what you want.
Crapping on PHP for things like function inconsistency (eg functions that use underscores vs those that don't, needle/haystack inconsistency) is a tired cliche and boring at this point. It really adds no value complaining about things that just don't matter and/or are historical.
My biggest grievance is that Hack (FB's fork) is basically better in almost every way. The collection types (vec, keyset, dict) are better (eg less weird coercion defaults). The type system is leaps and bounds ahead of anything PHP has. Nullability as part of the type system is amazing. It's a shame Hack didn't become the dominant dialect.
As for the book, I went through it and it's really solid. Good job to the authors and maintainers. It covers a lot of issues like register globals, sanitizing input, Unicode and so on. It also has links to more detailed information on any of these topics.
[+] [-] Darmody|4 years ago|reply
[+] [-] psychoslave|4 years ago|reply
But I did end up using CVS for seven years at a commercial company and I hated it with a passion. When I say I hate CVS with a passion, I have to also say that if there are any SVN users in Subversion, users in the audience, you might want to leave because my hatred of CVS has meant that I see Subversion as being the most pointless project ever started, because the slogan for Subversion for a while was, CVS done right or something like that.
And if you start with that kind of slogan, there’s nowhere you can go. It’s like — there is no way to do CVS right. So that’s the negative kind of credit.
https://singjupost.com/linus-torvalds-on-git-at-google-tech-...
[+] [-] omgitsabird|4 years ago|reply
I feel like you are stretching a quote to fit a premise that doesn't match the context of this thread or article.
[+] [-] tored|4 years ago|reply
https://www.google.com/search?q=c+the+right+way
[+] [-] akie|4 years ago|reply
[deleted]
[+] [-] rbanffy|4 years ago|reply
But, really, I suggest using other tools. We have nicer tools now.
[+] [-] unfocussed_mike|4 years ago|reply
Especially Laravel + Lighthouse, which is an extraordinarily neat GraphQL layer considering the huge deployability you get with PHP.
PHP is progressing towards a nicer language in a way that JavaScript, IMO, is not. And taking tens of millions of programmers with it.
[+] [-] that_guy_iain|4 years ago|reply
Debatable really. Within the web development space the options are rather limited in my opinion.
Options:
* Spring based apps - Downside JVM is a Resource hog
* Python, Ruby, etc - The web frameworks are in my opinion not as full featured. Same with CMS systems. Also requires more resources. Also have limited OO features.
* Go, Rust, etc - Really good choices for APIs but their web frameworks are also really lacking.
The tooling around PHP is really good and stable. The language itself, the downsides are very limited and the main one that remains valid is inconsistency of function parameters. The issues with bad code are generally the same ones that can be done in another langauge.
[+] [-] lpcvoid|4 years ago|reply
[+] [-] wraptile|4 years ago|reply
[+] [-] bayesian_horse|4 years ago|reply
[+] [-] tored|4 years ago|reply
[+] [-] chinathrow|4 years ago|reply
Citation needed.
For folks not wishing to switch frameworks and tooling every year, you can get a lot done with PHP.
[+] [-] fraktl|4 years ago|reply
[deleted]
[+] [-] throwtcp5327|4 years ago|reply
(The only proper) PDO tutorial: https://phpdelusions.net/pdo/
Organize a php project according to industry best practice: https://github.com/thephpleague/skeleton
PHP Best Practices - A short, practical guide for common and confusing PHP tasks: https://phpbestpractices.org/
Modern PHP Cheat Sheet - A to-the-point summary of all awesome PHP features: https://front-line-php.com/cheat-sheet
Laravel, PHP, JavaScript: https://freek.dev/
PHP security: https://paragonie.com/blog
[+] [-] faho|4 years ago|reply
One that baffles me is that 8.0 changed it so all scripts default to a "C" (i.e. english, ascii-only) locale. [0]
So this is now a scripting language that requires you to call `setlocale` for every script manually.
Contrast python, which tries to fix the locale so it's at least UTF-8 aware. [1]
[0]: From https://www.php.net/manual/en/migration80.incompatible.php "The default locale on startup is now always "C". No locales are inherited from the environment by default. Previously, LC_ALL was set to "C", while LC_CTYPE was inherited from the environment. However, some functions did not respect the inherited locale without an explicit setlocale() call. An explicit setlocale() call is now always required if a locale component should be changed from the default. "
[1]: https://www.python.org/dev/peps/pep-0538/
[+] [-] pas|4 years ago|reply
It practice all these env config initialization things are handled by the php.ini, and/or by a framework.
(Plus using the mb_string ext is how any minimally sane string handling is done in PHP nowadays.)
Yes, probably in ~10 years the PHP world too will move to a unicode by default setup :)
But hey, progress!
[+] [-] thriftwy|4 years ago|reply
The beauty of perl scripts, and later php templates, was in their BASIC-like simplicity. They were not secure or structured in any way, but very accessible.
Once you add these frameworks, you realize you could have started with Python or Java in the first place and get more mature infrastructure and better language along the way.
[+] [-] Isinlor|4 years ago|reply
[+] [-] rbanffy|4 years ago|reply
And, with things like Flask or FastAPI, you get BASIC-like simplicity and immediacy with a language that has a concept-space that scales with your understanding of it.
[+] [-] asadkn|4 years ago|reply
[+] [-] thriftwy|4 years ago|reply
Now with the cloud that advantage is utterly gone.
[+] [-] schipplock|4 years ago|reply
[+] [-] irq-1|4 years ago|reply
https://phptherightway.com/#php_and_utf8
This is a web language? PHP still has these kind of issues because of its culture: Get it done now and ignore any future costs.
[+] [-] WalterGR|4 years ago|reply
There’s a ToC on https://leanpub.com/phptherightway but I can’t link directly to it as the in-page link uses JS. (I’m on mobile though, so I can’t read the source. Maybe there’s an anchor someone else could link to.)
[+] [-] exikyut|4 years ago|reply
To answer the bigger-picture question, the first thing I see (the above-the-fold part) upon loading the page (on both desktop and mobile) is a stylized ToC.
[+] [-] jorams|4 years ago|reply
[+] [-] norman784|4 years ago|reply
I tried PHP last year (or should I say Laravel) and didn't found any good, the type system is bad for a project like Laravel, you have too many different files where you declare things and the type system doesn't now, so you end up without autocompletions (and warnings or errors from the php interpreter) you only catch those at runtime (I'm too used now to get the errors before run a project, or at least get a hind from the editor/IDE).
[+] [-] tonyjstark|4 years ago|reply
Laravel has also a pretty active and productive community and so many things just work out of the box, I’m not sure if Go or Rust has anything comparable.
I have to mention: I didn’t touch Go for over a year now and never used Rust. I felt that Go is pretty easy to learn in its basics but quickly throwing things together for a prototype with DB access, I would go for PHP at any time.
[+] [-] pluc|4 years ago|reply
[+] [-] haolez|4 years ago|reply
[+] [-] TekMol|4 years ago|reply
In other languages, you can import code like this:
The external code ("mail") does not have to make assumptions how it will be called when it is used.In PHP, it is the other way round. Every piece of code needs to try avoiding namespace conflicts by prefixing the code with something like this:
Hoping the namespace (here "Illuminate") will not clash with other code.[+] [-] ofrzeta|4 years ago|reply
Maybe not everything is "the right way" but at least the nullsafe operator is, I think.
[+] [-] unknown|4 years ago|reply
[deleted]
[+] [-] xet7|4 years ago|reply
[+] [-] mapmap|4 years ago|reply
[+] [-] jbverschoor|4 years ago|reply
[+] [-] porker|4 years ago|reply
I would _love_ to use asdf for everything, but outside a core set of languages/developers it doesn't seem to have the community around it.