PHP is a very popular server-side scripting language (a language that is not compiled, but interpreted), used by millions of developers all around the world. Like all programming languages, it has a history. It’s been through many different transitions, and was created to solve a particular problem. Unlike a lot of languages, PHP is highly criticized and even mocked, being called such things as a "double clawed hammer", meaning a broken or useless tool. Language wars have existed for years, with developers pointing to reasons why their favorite language is the best thing in the world and everything else is subpar at best. However, never before has the rest of the programming world been so united in ripping on and despising a language as they are towards PHP.
Despite this fact, PHP remains one of the most highly used and successful programming languages on the web. How did this happen? Let's start at the beginning.
PHP was created in 1994 by a dude named Rasmus Lerdorf. But this original version of PHP isn't very comparable to the PHP of today. Unlike most other languages, PHP wasn't a complete language from the beginning- it evolved slowly into a full language. Originally, it was a toolset for adding a dynamic layer to a website, so that users could interact with websites. It used a method called CGI (Common Gateway Interface), written in the C language. These tools were called "Personal Home Page Tools", or PHP tools. After this, Rasmus and others continued to add onto this toolset, adding more and more functionality, and changing the name several times until it became a complete programming language.
During the time when PHP was created, there was really only one way to serve dynamic content to users on the web, through Common Gateway Interface, or CGI, scripts. CGI involves writing a program that serves up web pages, which can include dynamic content. This is stuff that can change depending on who is looking at it, or that allows the user to interact with the website in some way, like filling out a contact form or filling out a website’s “guestbook”, which used to be a popular way for users to leave comments on websites. Although CGI is still used today, and is easy enough, programmers like to try new things and expand our understanding of what can be done. PHP can be thought of as an additional way to make dynamic web pages, and to do it as easily as possible.
One of the main features of PHP is its ability to be embedded in html. That ability took PHP away from being just another CGI tool (PERL was already doing that just fine) and enabled a different approach. Instead of building a web application to serve up html pages as well as dynamic content, PHP suggested that a website, mostly static, was built first and pieces of dynamic functionality could be added here and there as needed.
Here is a simple script written in PERL, which makes a database query and outputs html:
#!/usr/bin/perl
use Mysql;
print "Content-type: text/html\n\n";
$connect = Mysql->connect("dbhost", "dbname", "dbuser", "dbpass");
$connect->selectdb("dbname");
$myquery = "SELECT * FROM users";
$execute = $connect->query($myquery);
print "<html>
<head>
<title>Perl Sample</title>
</head>
<body>
<h1>List of Users:</h1>
<ul>";
while (@results = $execute->fetchrow()) {
print "<li>".$results[0]."</li>";
}
print "</ul>
</body>
</html>";
You can see that in the above example we never break out of the PERL script, the PERL script outputs the html.
Here is an example in PHP:
<html>
<head>
<title>Old School PHP</title>
</head><br>
<body><br><h1>List of Users:</h1><br>
<ul><br>
<?php<br>
mysql_connect("dbhost", "dbuser", "dbpass");<br>
mysql_select_db("dbname");<br>
$query = mysql_query("SELECT * FROM users");<br>
while($row = mysql_fetch_assoc($query)){<br>
echo '<li>'.$row['username'].'</li>';<br>
}<br>
?><br>
</ul><br>
</body><br>
</html>
In the PHP example, we start in html, then have some PHP inside the html document which performs a database query and then displays the result along with some html, and then exit the PHP script and jump back into html.
This model quickly took off. If the only dynamic content you need is a contact form on your website, a short PHP script to handle that form and send the email is one of the simplest and quickest ways to achieve this. If dynamic content needs to be served on a page, a PHP script can be written to display that content right on the page exactly where it should go. A database can be queried and the results displayed right in an html page, like in the example above. As this became more popular, more and more functionality was desired, and PHP slowly evolved to be able to do most anything that any full language can do. Its design was simply to slowly evolve to the needs of those who were using it.
The problems mostly come when a larger application is required. Using the traditional PHP model, based around a website where the pages are the center of it all, there really isn't any structure to the application or separation of roles between logic code and display code.This means that things can get really messy really fast.
Today, the PHP world is basically divided into two main camps: those who still use PHP as it was originally intended, and those who have taken PHP and adopted it to be used as other languages are used. Those of the second camp built PHP into an Object Oriented language, and continue to improve the language. Frameworks sprung up that are well designed, based on frameworks in other languages. Lately, more and more tools are styled off of things used in other languages, including PaaS solutions like Fortrabbit and Pagoda Box, and package managers like Composer.
But often times, the implementations of these "better" methods lack the ease of use that was PHP's greatest appeal, and can be less graceful and harder to use than the sister solutions in other languages.
The problem with the traditional method of using PHP to build an application is that there isn’t any structure to the application. A good application has some sort of design for how data flows inside the application, and what roles different parts of the application play. For instance, an MVC app is a very common pattern for designing applications.There are 3 main parts, the Model, the View, and the Controller. The View is the part that the user sees, usually composed of html files and whatever dynamic content the application generates. The Model is where the app does most of its work, and interacts with a database if there is one. Finally the controller ties it all together and passes data from the view to the model, and from the model to the view.
However, with the way that PHP was originally intended, a nice structure like MVC is impossible. Instead, everything is based around a "page", a page being something the user lands on. All the code is usually broken up into each of these pages, making the flow of data around the application very unpredictable and hard to maintain. It also means that the programmer has to constantly repeat parts of the same code. Instead of having a specified model that handles fetching data from a database, raw database queries are written right into the same file that is generating the code that the user sees, so that code has to be repeated all over the place.
PHP became popular for its ease of use, its simplicity, and ease of deployment, but it is staying around because of entirely different reasons. Entire large frameworks are now built with PHP, (such as Laravel, Zend, Symphony, and many more), in Object Oriented style, with a completely different philosophy than PHP started with. Some of these large frameworks are so large and complex, they can’t be run on cheap, shared hosting plans because of their architecture, which is what PHP hosting was all about for a really long time.
Ironically, the very things which make PHP unique from other languages, are the very things that the most progressive forces in the PHP community are seeking to get rid of. As the language continues to evolve and improve, it also loses its originality and main features.
The above example which used the mysql_ functions is now deprecated. In a modern PHP app, the view would contain only the html, there would be no database calls mixed in with the html. Instead, a model would grab information from the database, and the controller would pass it to the view. What this looks like would greatly vary depending on the framework used, but here is an example of what the view might look like, assuming that the controller passed an array of users to the view:
<html>
<head><br>
<title>View</title><br>
</head><br>
<body><br>
<h1>List of Users:</h1><br>
<ul><br>
<?PHP<br>
foreach($users as $user){<br>
echo "<li>{$user['username']}</li>";<br>
}<br>
?><br>
</ul><br>
</body><br>
</html>
One of the ways that PHP is most used on the web today is in various CMS platforms. There are tons and tons of different Content Management Systems out there, some with better structures than others, but some of the most common are Wordpress and Drupal. These are a middle ground between an old school approach and a modern PHP web app. The internal structure of these systems is not always pretty, and a lot of the templates and plugins use spaghetti code or a lot of html mixed in with the PHP code, both logic code and display code, all mixed up together. However, these systems do not use deprecated functions like the msql_ extension.
Love them or hate them, CMS platforms like WordPress are insanely popular, with wordpress alone now powering something like 20% of the web.
PHP is a unique language that takes a different approach than any other programming language. But that approach is becoming more and more outdated and being replaced within the PHP community by patterns copied from other languages. This improves the language, but removes its uniqueness. PHP, having evolved from the needs of those trying to bring dynamic functionality to websites, is now being redesigned retrospectively based largely on what other languages can do. PHP is a tool that allows the programmer to build clean apps with good structure, but can be used easily to build horribly unmaintainable and sloppy, messy apps. It's up to the programmer to decide how to use it.