I’ve been talking about PHP a lot recently, but so far I’ve said little about what it is and how it works.
So, What is PHP?
It’s a programming language, used to greatly increase the capabilities of a website. In this way it’s very comparable to JavaScript, and can even be seen as its complement. Both are ‘scripting’ languages. Unlike ordinary programs which are written in human-readable code and then ‘compiled’ to the binary instructions that computers can execute, scripts stay human-readable. Their code is executed by an interpreter program. With JavaScript, that program is your Web browser – any script in a web page you view actually runs as a program on your own computer. PHP however is interpreted by the PHP program, and this must be installed on the computer where the web page is held – the web server. For this reason it’s described as a ‘server-side scripting language’.
PHP files are a lot like HTML web pages, but also contain these PHP instructions or ‘scripts’. In normal Web browsing, the user requests a file by clicking on a link to it. If this is HTML it is sent directly to them, to be rendered on the screen as text and images by their browser. If it’s a PHP file however it is sent first to the PHP interpreter on the server. Here the scripts it contains are executed and the results – which are always HTML – inserted into the document where the script used to be. Now entirely HTML, it is sent to the user’s browser to be rendered in the usual manner.
So What Can They Do There, These Scripts?
It’s really just a programming language like any other. If you already know one of the more popular ones – C++, JavaScript, Perl – it will all look pretty familiar.¹ So you could use it for any computational purpose imaginable (it is ‘Turing complete’, as the theorists say). I wouldn’t though. What makes PHP special is not so much what it is as where it is: Sitting between the HTML and a database on a web server, allowing one to talk to the other.
In this way PHP can take requests from a website user, turn them into SQL or some other database query language, and format the results of the query as HTML to send back to the user’s browser where they can be displayed. As I mentioned in the previous post (and the one before that), this is a hugely powerful and flexible technique that can be used for untold purposes. While pages of search results would seem an obvious example, that’s just the nursery slopes. Systems as complex as Facebook are built in PHP. And systems that are good too.