PHP Introduction

Chapter 1 33 mins

Learning outcomes:

  1. Frontend vs. backend technologies
  2. What exactly is PHP
  3. What is PHP capable of
  4. History of PHP
  5. Statistics of PHP
  6. The official documentation site

Introduction

Over the years, as web development has gradually taken charge as one of the most common programming activities, so have the technologies that are involved in it.

Web development is a huge and extremely diverse field of computer science. There is just so much to talk about in this arena that even a hundred books can't account for it. Seriously!

There are tons of technologies and tools available in this field that solve many common development problems and, in general, make programming the web easier, intuitive and efficient.

Now generally speaking there are two broad categories that can filter out this huge arsenal of technologies and tools, following from the context in which they help developers.

We have frontend technologies and backend technologies.

The frontend category essentially hosts everything that goes into building the frontend of a web application, i.e. what the user sees on his/her computer.

Some common frontend technologies are HTML, CSS, JavaScript (well, who can miss on them?) that form the core of the web, in addition to helper technologies such as Bootstrap, React, Vue.js, Angular.js, SASS and so on.

On the otherhand, the backend category hosts everything that is concerned with what happens and how that happens on the backend of a web application, i.e. the place where the application is hosted, also known as the web server.

Some common backend technologies are Python, Ruby, Java, Go, Redis, MySQL, MariaDB, PostgreSQL, MongoDB, Perl, and you guessed it — PHP.

Before beginning to learn any new technology in the world of web development, it's paramount to know exactly which category does it fall in.

PHP, in this case, is a backend technology that is intended to create robust web applications.

But this isn't much helpful in understanding what exactly is PHP.

You might've heard a lot about PHP such as that 'it's a recursive acronym', or that maybe 'it stands for Personal Home Page', or that 'it is a scripting language', or that 'it enables one to create login and registration features on web applications', or you might be completely new to PHP, having heard absolutely nothing previously.

Whatever the case is, it's finally time to detail extensively as to what exactly PHP is and what it is not. Having this knowledge is essential so that we know what part of the equation are we currently working on and that what features could be naturally available to us.

What is PHP exactly?

To start with, let's settle on the fact as to what does the word 'PHP' stand for.

PHP is a recursive acronym for PHP: Hypertext Preprocessor.

A recursive acronym is an acronym that contains the acronym itself in its full form.

Notice the term 'PHP' in 'PHP: Hypertext Preprocessor' — that's the acronym being used again in the full form of the acronym itself.

You might think as to what's the point of using a recursive acronym.

Well, one reason might be the history of PHP which we'll touch on very shortly later. A second reason might be because it seems cool. A third reason might be because 'HP' (for 'Hypertext Preprocessor' simply) would have lead to copyright issues with a company having the same name.

Nonetheless, one thing that's clear is that PHP stands for 'PHP: Hypertext Preprocessor', and being an acronym, we must write it in capital letters as 'PHP'; not as 'Php'.

What is meant by 'Hypertext Preprocessor'?

As for the meaning of the term 'Hypertext Preprocessor' in the full form of PHP, the language is in a lot of ways a preprocessor for HTML (which is also known as hypertext).

A preprocessor is simply a program that reads in a text file and makes certain changes to it, such as adding new information or removing given information. The file is then actually processed (which usually means to parse and execute) by another program.

Hence the name 'preprocessing' — it's processing done before the actual processing.

Hypertext, as we all know of it, is parsed by a browser software. PHP isn't by any means an HTML processor — it doesn't execute HTML code (in fact, that would be senseless).

It parses special tags where PHP code dwells, executes the code within these tags and, if asked to, injects text into the location of the tags. So it preprocesses HTML files by adding content to them which are then delivered over to web browsers.

Moving on, by nature, PHP is an interpreted, scripting language introduced to the world in 1995 by Rasmus Lerdorf.

Being 'interpreted' means that it isn't precompiled into machine code in the form of an executable file; rather it's executed directly at runtime.

Furthermore, being a scripting language means that it runs inside a virtual environment automating computational tasks. This virtual environment where PHP is parsed and executed, also known as the engine or implementation of the language, is actually just another computer program, written in some other programming language, typically C or C++.

Today, almost all of the PHP in use around the world is driven by the Zend Engine.

Zend Engine is an implementation of PHP written in C. Its founders are Zeev Suraski and Andi Gutmans. The engine is open-sourced; the entire code is available on GitHub under the php-src (PHP Interpreter) repository.

What is an implementation of a language?

A computer language is merely an idea, an abstraction, for talking to a computer. PHP is an example of a computer language. So is C, so is JavaScript, so is Python, and so on.

A language on its own is just a set of grammatic rules and their meanings, nothing more that that. A program that is capable of understanding a piece of text written in a computer language is called an implementation of that language.

The implementation is free to use any methodology to bring the mere lines of text of the language to life by executing them on the underlying machine, or at least converting into a format that can be readily executed by another program.

Some common methodologies are listed as follows:

  • Compiling the program into native machine code, as happens with C and C++, or into some intermediary language, as happens in Java.
  • Interpreting the file in the old-school way, reading each statement sequentially and then executing it
  • Taking a hybrid approach, bringing the best of both the aforementioned ideas into the equation, often referred to as JIT compilation. JIT compilation is used by the V8 engine to execute JavaScript and by the Zend Engine to execute PHP.

There maybe, and usually are, numerous implementations of a language. Theoretically a language's implementation could be written entirely in the language itself and many actually have been!

In our case of PHP, Zend Engine is the implementation of PHP, i.e. it's a piece of software that can understand and ultimately execute PHP code. It's written in C (which itself is a compiled language).

Other implementations exists such as HHVM, PeachPie (written in C#), Parrot and so on.

Besides this, PHP is a high-level programming language. This means that PHP abstracts away many of the complexities of programming a computer machine. PHP code is simple to write, easy to understand and resembles English language to a great extent.

The best way to understand the benefits of a high-level language for coding is to contrast it with a low-level language.

Assembly is a low-level language. Coding in assembly, if you've ever done it, is exceptionally tedious — like seriously! We don't get many of the facilities of a high-level language such as variables, functions, structured control flow, operators, and so on, that otherwise help us reduce the verbosity of the code and make it intuitive to understand.

In today's world, almost all of the popular computer software that we are aware of are built in high-level languages including JavaScript, Python, Ruby, Go, etc.

Moving on, when we talk about a programming language, it's fruitful to know about the styles of programming — or better to say, the paradigms or programming — supported by that language.

Over the years of technological advancements in computer languages, many paradigms have sprung up, thanks to the work of many notable computer scientists.

PHP came in a time when structured programming what already on the forefront, henceforth, it's not surprising to know that PHP naturally supports structured programming. Apart from that, it supports procedural, object-oriented, and the functional paradigms.

If you've heard of these terms for the very first time, don't get scared by their fancy names! As you'll embark on the journey of writing web applications and other pieces of software, you'll gain a better understanding of all of these paradigms, their purpose, their pros and cons, etc.

PHP's syntax is heavily influenced by C and C-like languages (such as C++, Java, Perl). Therefore, if you're coming from a C background, or any other C-like language, you'll pick up the syntax of PHP in the matter of seconds.

Believe it!

Let's now explore the real potential of PHP, i.e. what it can really do.

What can PHP do?

PHP isn't considered by some as a powerful and expressive language, yet it offers a huge code base capable of doing almost any computational task one can imagine. Moreover, it has a huge community of developers, frameworks and technologies to further strengthen the language.

PHP is not an easy competition by any means and, even to date, powers most of the servers out there. (The real stats are just about to come.)

To fascinate you about the potential of PHP in the world of web development and even outside it, here's a short list of things that can be done in PHP:

  1. Create and edit images on the fly.
  2. Process PDFs.
  3. Talk to databases and thus create login/registration forms, blogs, CMSs (Content Management Systems) and so on.
  4. Run CLI programs.
  5. Create files and directories on the file system.
  6. Talk to an HTTP server as a client.
  7. Process HTML forms.
  8. Execute shell commands and other processes on the operating system.
  9. Work with cryptographic functions to sign data.
  10. Be used to parse JSON and thus build REST APIs.
  11. Read cookies, work with sessions and other HTTP-related features in a seemless way.
  12. Write custom HTTP headers, retrieve them, and perform request routing.
  13. Create GUI (Graphical User Interface) applications.

The best part of all of this is that everything that we've talked about above is built right into the language — there is no need to wander around trying to download external packages or extensions (for most of the features).

In the starting part of this course, we'll primarily concern ourselves with working in the CLI.

Now what exactly is the CLI and why we prefer to use it to teach PHP is deferred up until the next chapter. For now, let's get to learn more about the early days of PHP.

It's time to go back into time...

A tale of rewrites

Rasmus Lerdorf, a programmer had created his online resume website where he wanted to track the count of visits to his site. For this, as was common back then, he wrote a set of CGI binaries in C that were capable of parsing special pieces of text (this was the very first PHP syntax) embedded inside HTML comments.

So in one way, the whole set of CGI binaries was simply a kind of parser able to interpret the scripts containing special commands.

Rasmus called these binaries collectively as PHP Tools for Personal Home Page Tools, as they were helping him perform computational tasks on this personal home page, easily.

The idea was a good one but not a wholly new one. There were already technologies like PHP Tools and, in fact, much stronger and much more mature — the likes of Perl.

However, PHP Tools wasn't conceived as a competition to Perl or to other CGI technologies, or even as a full-fledge scripting language in its own right — it was only created to solve one's personal web development problems.

In June 8, 1995, Rasmus released the code of PHP Tools to the public. As a result, the dev community got involved in this simple, still mainly a personal, project, improving certain features and dealing with bugs.

Soon another version was released which was called Forms Interpreter.

The name Forms Interpreter simply followed from the idea that the CGI tool was capable of parsing HTTP <form> data sent to the server and providing it as simple identifiers in the associated script text. Now it was easy to determine any GET parameters or any POSTed collection of form data.

Besides this, there were other features as well in this version of the tool.

Moving on, Rasmus decided to do a complete rewrite of the tool to add more functionality in it. In October 1995, he released this rewrite of the tool and named it PHP Construction Kit, bringing back the 'PHP' term.

By this time, Construction Kit was an advanced scripting tool, however still only limited to Unix and Posix-compliant systems. A lot was still missing from it, and soon, as we shall see, to be filled into it.

Having come so far with the release of Construction Kit and still not satisfied with the many features of the tool, Rasmus decided to do yet another rewrite.

Yes, that right, another one!

Well, it might seem strange, but this is typical in the ever-evolving world of programming. We build something today with some mindset and purpose; that thing rises up its summit, gains traction and is soon used in many ways not originally intended and designed for; that's where code refactoring has to be done or, as in the case of PHP, a complete rewrite in order to make that thing efficient and at the same time make the code all simple and robust.

In April 1996, this rewrite was released and named PHP/FI for Personal Home Pages/Form Interpreter.

As is evident, the names of previous releases had finally been brought together. This version of the CGI tool was officially given a version status of 2.0 — hence PHP/FI 2.0.

PHP/FI was way more powerful than its predecessors, having such features as database support, cookie handling, user-defined functions and much more. What's interesting to know here is that this 2.0 version didn't receive any incremental patches as do software typically (such as version 2.1, or 2.0.1). The one and only version ever released was 2.0.

This was because soon another rewrite of the tool was on its way, about to change everything from PHP/FI 2.0. Nonetheless, PHP/FI 2.0. was powerful and simple and due to this very fact, saw itself deployed in many web servers.

Quite expectedly, the story doesn't end here. In 1997, dissatisfied with the features and efficiency of PHP/FI 2.0, two university students working on an ecommerce application for a university project decided to do a complete rewrite of the underlying parser and reformulating the scripting languages syntax.

They approached Rasmus online, discussed about these new ideas and soon began on the journey for a completely new and rejuvenated scripting language for the world — PHP.

They called the language PHP, bringing back its very first name, but this time not to mean 'Personal Home Page' but rather 'PHP: Hypertext Preprocessor' (the acronym we saw at the start of this chapter).

A team of developers including Rasmus, Zeev and Andi worked on this new scripting language and finally released what was called PHP 3.0, in June 1998, to the public. In less than a year since this release, PHP 3.0 found its way in the memories of over thousands of web servers and was no more a only-POSIX-compliant technology.

If you think this was the last major rewrite of PHP from the ground up, well that's not quite actually what happened. In late 1998, shortly after the release of PHP 3.0, Zeev and Andi began yet another complete rewrite of PHP's core.

But why now, you may ask?

Well, here's why...

The large collection of features provided by PHP 3.0 led to the development of extremely complex applications that weren't efficiently executed by the underlying PHP parser. This can't be called a design flaw, but a lack of oversight as to how much could the language really be used for. Zeev and Andi may not have imagined such complex applications using PHP in complex ways, but once they saw that in practice, they went on to solve efficiency problems in their implementation of PHP, along with adding yet even more features.

They called this new implementation the Zend Engine (derivded from their names: 'Ze' from 'Zeev' and 'nd' from 'Andi').

In mid 1999, Zend Engine was released and along with it PHP version 4.0. This was an extremely efficient and powerful implementation of PHP with notable new bells and whistles such as HTTP sessions, output buffering, more language constructs and so on.

The result was, well it's not hard to guess, more and more adoption of PHP across web servers.

Almost after a timespan of 5 years, PHP 5.0 was released with Zend Engine 2.0, once again with a wide array of new features and performance gains.

And since this release, PHP has seen active development, thanks to its large community of developers. Attaining newer and newer versions over the years, PHP today is on a level whereby competition is no easy feat. It's a mature, powerful, highly efficient, yet simple language.

Into the stats

The last thing before finally beginning to start working with PHP is to gain an insight into its statistics i.e. its usage across web servers, the average salary of a PHP developer; scope of PHP in the world of jobs and so on.

First, let's start with an estimate of the widespread usage of PHP across web servers. According to W3Techs,

PHP is used by 78.0% of all the websites whose server-side programming language we know.

That really is a staggering figure. What it means is that a large percentage of the web is powered by PHP and likewise it's there to remain in the future as an important server-side technology.

As for the customers of PHP, it is used by Facebook, Wikipedia, Wordpress, Drupal and Joomla, to name a few. This is by no means a comprehensive list of the big names out there — there are too many of them!

But at least these few names might be enough to convince you that PHP isn't a toy capable of doing only simple tasks — it's a giant and as of the recent version 8.1 that appeared in early 2022, it's already a highly performant language with the sophisticated Zend Engine 4.0 implementation.

Moving on, when we talk about PHP, it's worthwhile to talk about some common frameworks that surround it for ease in web development. Frameworks include Symfony, CodeIgniter, Laravel, CakePHP, amongst the most notable ones.

These frameworks are in mainstream use today and and power many many web applications, some even the scale of large enterprises. Some frameworks are still being actively developed and improved, and this just

As per the salary of PHP developers, although it might not be as much as some other specialized technologies, it's still a very good figure to look at.

According to Payscale, the average PHP developer salary is close to $65k per annum.

As we go higher up the experience level, the salary goes up quite a bit. The average salary of a highly-experienced PHP developer with more than 5 years of experience gets somewhere between $80k – $100k.

Here are a couple more resources to dig even deeper into the salary distribution of PHP jobs:

Obviously this varies with location and company, but at least we can expect decent salary figures from a technology that's superbly easy to learn and apply in practice.

The official documentation site

Nearly every computer language out there has some sort of online documentation for it and PHP is no way behind.

The documentation of PHP, which is a very well-written one, is available at the official website: php.net.

php.net — PHP's official documentation site.
php.net — PHP's official documentation site.

In this course, we'll routinely refer to this documentation site to go through the syntax structure of functions, keywords, classes, and get other helpful insight related to given features of the language.

As a programmer of a given language, one should clearly know as to where to look for documentation of the language. Undoubtedly, it's one of the most important things to be aware of while learning, and even using, a language.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage