Yes, we will discuss the future of PHP. I will tell you my views and may be you will tell me yours (in comments). However, before that happens, I would like to explain myself a bit. I did not want to write this post for the sake of writing. By “for the sake of writing” I mean, it is not something that I felt compelled to share to make my heart feel light. This post is a reaction.
I happen to be the kind that mostly reads others’ answers but does not write too many. As it happens, my Quora feed is full of people asking why and when PHP is going to die. Whenever I read that, I wonder why? Why after all some people think that PHP would die in the first place? People give a lot of arguments about it. I would like to present, and counter some of them. I think after having worked on PHP for the last 7 years, I qualify to talk a bit about this. If you are here for a one-word opinion, it’s NO (I do not think PHP is going to die anytime soon). If you are interested in the reasoning, please read on.
PHP is Old
We the social, intelligent mammals learn about death in so many ways many-a-times; from the death of a pet dog to statistics about cancer deaths and yet, the single biggest reason of death remains natural ageing. Maybe we as a species have gotten this equation of “Old = Soon to die” ingrained too deep in our thinking system. PHP is not a person. It is a languages and languages do not die. At best, they become obsolete and irrelevant.
PHP, like any other language is built around a philosophy, an idea. No one says Java is going to die even though it is only about 17 days older than PHP! If PHP is old, so is Java. Well, what about C? If PHP’s time has come already, C should have been dead long ago and yet, it remains seated in its throne, undisputed as the ruler of performance and the base of all practical operating systems.
PHP is slow
Indeed, it is. PHP5 is slower than PHP7. But PHP7 beats most scripting languages hands down in performance. There are enough benchmarks out there. This one made me open my eyes wide. I remember looking at it after PHP 7.0 was released. You can search for comparisons against other languages too. So PHP is not slow at benchmarks. Interestingly enough, benchmarks are not the whole scene. The application is the real scene, where languages get tested in the real world. PHP7 has fared well and comes close enough to HHVM in real world tests too.
I’d politely suggest you to read “PHP is slow” as “PHP was slow”. Please be considerate with people who are oblivious about PHP7’s existence and good adoption rates. Also, PHP8 (whose roadmap is not yet frozen or clear) is supposed to have a JIT in the opcache extension. If it is slower at certain points, they would be addressed, hopefully.
PHP is only for the WEB!!!
PHP stands (recursively) for PHP Hypertext Processor. At one point of time, early in its days it was called Personal Home Page. I am not sure but I think some folks from GNU got to the PHP guys and renamed it. As such, it was devised and designed for serving web pages. Pretty right, PHP is for the web. But that’s not where PHP’s capabilities end. It can very well be used to write a socket server, a small web server (see this, this, this and this), reactive event-driven non-blocking services, DNS resolver, websocket server and whatnot. If you want to develop command-line tools, there’s something for that too!
PHP is not for the web only, anymore. It can be used to build almost anything on the server-side. Now, don’t you start making plans for building a database written in PHP.
PHP is single threaded
Start here to throw away your belief about absence of multi-threading in PHP. Now, PHP is neither the most elegant, nor the most mature language when it comes to multithreading, but if you are ready to rub the sweat off your brow, you have the tools for the job. Remember though, that a lot of popular programs are not dependent on the multi-threaded paradigm. Multi-threading is not always required and opens up another set of problems as well.
PHP does not have advanced features
Most people who say so have no clue what they are saying or have not updated themselves in a quite some time. PHP has almost all the features other scripting languages have – namespaces, traits, generators, closures etc. If you say “I do not like PHP because it does not have multiple-inheritance and traits are ugly”, then you will not like Java either because Java does not have multiple-inheritance either. That does not make Java a bad language. PHP has most features other languages have except some that might not fit its design right now (like Generics).
As of PHP7, you can have stricter code with data types being set for function returns and parameters. You can have variables without types, like how you always had. You can mix and match according to what your application needs.
There is yet another army of developers who think that other scripting languages are better at certain tasks like image manipulation, XML parsing, multimedia encoding etc. Well, as I see it (and my sight might be poor, so excuse me if it is) most scripting languages are crawlers when it comes to high-performance code. Most such high-performance jobs are written in either C, C++ or Java (ideally C/C++) and what most scripting languages do is use an extension to talk to the high-performance library and get the job done. Take ImageMagick or FFMpeg for example – you have bindings for them in both PHP as well as Python. If you were to use them (and they are one of the best in their class, so why wouldn’t you) you would install the extension for the language and call the functions provided by the extention – very much like connecting to databases.
PHP has security issues
People complain about how GET and POST variables behave in arrays (used to get values for checkboxes from a page) and how they can be used to bypass the security of a script. Truth be told, if you are writing PHP code without using one of the frameworks out there, you are either a newbie or an absolute expert. As a newbie, you don’t get to complain about the language; you instead need to understand the design (more on this later). An expert would not make that mistake anyway. Now, I get your complaint but, hey, this is a language-level thing. Get some tools.
I’ll agree that PHP’s design was bad up until PHP7. Even in PHP7, there are certain points which have not been addressed and there are function names and parameters which are mind baffling for their naming and parameter orders. Thankfully, there are IDEs. If I were to be left without both internet connection and an IDE, my 7 years of PHP would start looking lesser by almost an year. I just cannot remember which one of array_search and strstr has the needle as the first argument. PHP’s naming convention for function names and order of parameters are in a disarray.
The web and web alone
When it comes to number crunching, PHP is not widely proclaimed as winner. Here is a question: Is PHP not suitable for scientific computing? Well, the answer is – No. It is. But the most popular AI and ML libraries are written in Python. Now, if you were to benchmark PHP against Python, Python will lose on performance. But since the libraries started getting build in Python, the trend grew and went on growing. So as of now, there are many AI/ML/Data Mining libraries out there in the market. If you know the math, you can build one. But apart from performance, there would hardly be any reason to.
I am not a believer that you should re-write everything there is in every possible language unless there are significant performance gains.
Coming back to the topic – PHP was designed for the web primarily and that means, it has a web-specific design pattern which contains a couple of very important features. Allow me to explain:
- The Web is stateless: The web is not a ‘connected always’ design. You open a webpage and then you can close the page. Neither your browser, nor the server maintains states about what you had done (websockets are bit of an exception, but then, are they?). PHP was designed to handle this scenario ground-up. Typically, when you request a page, the server (Apache/Nginx) listens to the request, calls up PHP (either as a module or via PHP-FPM). PHP serves the request and the script dies off. No traces are left for its run (unless you have set cookies and the database was altered during the execution). PHP is stateless in that regard.
- The web is very stringy: At one point of time, I attempted creating a small C server as an experiment to see where I could get. I looked around for some help on the web and got to write some code which could act as a CGI server. And man, it was difficult! Processing stuff in C means you have to take care of the data types very, very strictly. All parameters recieved via HTTP are delivered as strings. To convert each of them into numbers explicitely is a pain. PHP’s intrinsic type conversion ensures that you just treat arguments as numbers (float/int) or strings when you want to.
- Yummm, Cookies!: There are a few things that in some ways characterize web development – Cookies, GET/POST data and Sessions. In the days when C was what there was for programming most of the time, GET request was something that was passed to the CGI program as ‘command-line argument’ (thus it’s 64K limit on length) which POST was something which came in via STDIN. Would you like to write programs in such a manner, especialy compared to having two ready-to-use (super)global variables? Would you like to construct special cookies and add them to the response header yourself, rather than using another variable handy for use? Or would you like to handle user sessions manually? You see, there are things which are so common on the web that you forget about them – PHP has those built right-into-the-language (can’t say it enough number of times).
Apart from that, collecting server’s info, parsing and constructing URLs are all built right into the language – you don’t need libraries for that. All data types and methods are just too well suited for that. The array acts as lists and dictionary. Add the word ‘const’ and the array can act as a tuple.
In addition, memory overload protection, excessive processor usage protection etc. are built right into the system to help you ensure that a single request which triggered an infinite loop or one which asked for 21.743 terabytes of memory does not bring down the server (yes, tests, they are important for this reason). You do not need anything on the library layer. The language is well equipped for that.
PHP is probably not the best language of al time. I would much rather sway with swift (that’s a lovely programming language, but more on that some other time) than with others unless there are specific tools built in them that I really need and they are not written in any other language. Yet another reason why I would want to ditch PHP is because of performance.
Performance. That’s right. You see, if I write a program in a language and it gets a job done in minute while the same algorithm in another gets the job done in a few seconds, I would choose the one which runs faster. Because when it will come to huge workloads, I will be saving money. For complicated modules, my tests will run faster and the development will thus be at an accelerated pace. Right now, PHP is the fastest one (except Node.JS in certain cases) and that means I am sticking right to it.