Although PHP didn’t make it to the top ten most popular technologies in 2021 according to the StackOverflow Developer Survey, it’s nearly there – on the 11th position. It’s a fairly popular programming language and, on top of that, it’s considered one of the easiest to learn.
It’s been present in the software development world for over 25 years now, and it doesn’t come as a surprise that there’s a fair amount of common myths circulating about it. In this article, I’d like to zoom in on some of the misconceptions or outdated info. But let’s start with a brief intro to PHP and its history.
Table of contents:
2.1 PHP is not object-oriented / PHP is for procedural code only
2.2 There is no modularization in PHP
2.3 PHP doesn’t throw exceptions on errors
2.4 PHP is weakly typed
2.5 PHP code is mixed up with HTML
2.6 PHP has heavy frameworks
2.7 PHP uses global variables
What is PHP? Then & now
PHP was created in 1994 as a simple CGI script for displaying time and tracking visitors on its’ creators’ home page. Since then, the PHP programming language has come a long way and changed beyond recognition.
Object-oriented approach, type declarations and typed properties, significant performance upgrades, and Just-In-Time compiler are just a few and the most noticeable features added in recent years.
PHP is now responsible for over 78% of sites on the web, ranging from personal blogs through advanced e-commerce platforms to fin-tech portals.
Some common myths about PHP – let’s bust them!
Anyone researching PHP will come across many different opinions about it, good as well as bad. Among the latter, there are many myths and misconceptions that arose around PHP through the years.
Let's take a look at some of the most common among them.
1. PHP is not object-oriented / PHP is for procedural code only
“PHP applications are all spaghetti code functional programs”
That's outdated information. OO elements were introduced in PHP 5.0 and largely improved in 7.0. Objects, classes, interfaces, inheritance etc. – they are all there! You just have to want to use them.
However, it is still possible to write procedural code! Since PHP has no entry point, no `main` function, it is being run line by line so there’s absolutely nothing stopping you from just writing the business logic right there. Nothing but common sense that is.
It is worth noting that not everything is an object in PHP, which might be a cause of confusion for some people expecting different behavior. Primitive types are always passed by values to functions, but objects are being passed by reference.
2. There is no modularization in PHP
“All PHP applications are just a mess of libraries including one another”
Again: a thing of the past (although very true at some point). Back in the day, if you wanted to use any library, it had to become a part of your app and it’s usage was strictly bound to its location in the code.
Since PHP 5.3 introduced namespaces, that is no longer the case – the namespace identifies the class, you just point out which one you need, and all the importing is done by PHP. Sounds obvious but it wasn’t until 2009.
Along with namespaces came the ability to have all your external libraries managed independently from your code. And that’s where Composer comes in! Introduced in early 2012, it’s a PHP-based package manager for PHP applications. All libraries can now have their own dependencies which are being automatically resolved and can be updated with just one command.
3. PHP doesn’t throw exceptions on errors
“When you mess up, the end user gets an error message on their screen”
PHP is not throwing exceptions when an error occurs in the code – that’s correct. Instead, it triggers an error. And it’s not true that you can’t intercept those, you just have to configure the error handler to gracefully exit your application.
And even that behavior has changed in PHP 7. Since its release, most errors act like exceptions, and can in fact be caught and properly handled.
Since PHP does not compile to a binary and is loosely typed, it’s quite easy to, for example, mismatch functions arguments with its declaration if you don’t do things the right way. It’s not something you should be able to recover from – it’s just bad code.
4. PHP is weakly typed
“You just never know what is hiding in the variable”
PHP is a dynamically and loosely typed language. That means that the variable type depends on the context you use it in, and can change when passed to a function depending on the function’s arguments.
While this behavior hasn’t changed over the years for the variables, it has definitely changed for function arguments and object properties. Those can be strictly typed and default type casting can be disabled.
There is another issue often being addressed: type comparison. A commonly used double equation sign will compare two values while performing type casting. Adding a third one will perform type comparison first – a functionality most people would expect.
5. PHP code is mixed up with HTML
“PHP code is just a visual mess”
It’s another misunderstanding coming from the old days when websites were actually written this way. While you can still do this, no one really does. With templating languages like Twig and Smarty being available for years now, it’s more than reasonable to not do things the wrong way.
Over time, less HTML-generating applications are written in PHP. With the rapid growth of mobile apps, along with the rise of more advanced front-end frameworks, fully rendered web pages are no longer the main usage of PHP. Instead, most of the work is now generating JSON API responses.
6. PHP has heavy frameworks
“Frameworks are just huge and slow – you’re better off writing everything by yourself”
First PHP frameworks were all-in-one packages with database connections, ORMs, templates, form builders, CLI tools, even whole CMS all bundled together. Whether you needed them or not – you had to have them in your application.
While some frameworks still take on this approach, it is not a standard now. With micro frameworks available, you can start with just a command line application and add only those components that your application truly needs.
7. PHP uses global variables
“PHP is not secure because most data is placed in globally available variables”
That ain’t a myth actually. For the sake of backwards compatibility, superglobals and global variables still exist and can be easily accessed. The problem here is that they are not used as often as people seem to believe.
Retrieving POST data or other request parameters no longer requires digging directly into the superglobal variables. There are a couple of libraries that create wrapper objects around those, allowing an easy-to-understand and, most importantly, object-oriented and strictly typed approach.
PHP myths – wrap up
PHP is often described as a language easy to learn for beginners, mostly due to its forgiving nature of a dynamically and loosely typed language. It’s also very rewarding in terms of results achieved after a short learning time.
While the first lessons in most languages consist of reading a CLI input and/or formatting some output, first PHP projects are often (simple but still) fully working websites with, for example, a guestbook.
That leads to many applications being written by people without any programming background, which simply leads to bad code – not always, but often. And this code, still available online in the form of whole apps, snippets and event tutorials cementing bad solutions, is what gives PHP its bad reputation.
Mind you: none of the myths described here are truly false. They just address issues of the language that are either long gone or their influence on modern applications is exaggerated.