Introduction to PHP
In the mid-1990s the World Wide Web was in its infancy but becoming more and more popular. For the most part, web pages contained static content: articles and text that was “just-there.” Web pages were far from the fully interactive and dynamic applications that they’ve become. Rasmus Lerdorf had a home page containing his resume and he wanted to track how many visitors were coming to his page. With purely static pages, this was not possible. So, in 1994 he developed PHP/FI which stood for Personal Home Page tools and Forms Interpreter. This was a series of binary tools written in C that operated through a web server’s Common Gateway Interface. When a user visited a webpage, instead of just retrieving static content, a script was invoked: it could do a number of operations and send back HTML formatted as a response. This made web pages much more dynamic. By serving it through a script, a counter could be maintained that tracked how many people had visited the site. Lerdorf eventually released his source code in 1995 and it became widely used.
Today, PHP is used in a substantial number of pages on the web and is used in many Content Management System (CMS) applications such as Drupal and WordPress. Because of its history, much of the syntax and aspects of PHP (now referred to as “PHP: Hypertext Preprocessor”) are influenced or inspired by C. In fact, many of the standard functions available in the language are directly from the C language. PHP is a scripting language and is not compiled. Instead, PHP code is interpreted by a PHP interpreter. Though there are several interpreters available, the de facto PHP interpreter is the Zend Engine, a free and open source project that is widely available on many platforms, operating systems, and web servers. Because it was originally intended to serve web pages, PHP code can be interleaved with static HTML tags. This allows PHP code to be embedded in web pages and dynamically interpreted/rendered when a user requests a webpage through a web browser. Though rendering web pages is its primary purpose, PHP can be used as a general scripting language from the command line.
What is PHP?
PHP is recursive acronym for PHP: Hypertext Preprocessor. (Fun fact: PHP originally stood for Personal Home Page.) It is a general-purpose scripting language that is especially suited for web development and can be embedded into HTML. It is very simple of learn for a newcomer, but offers many advanced features for a professional programmer. It can be used on all major operating systems including Linux, Windows, and macOS.
Hello World in PHP
<?php printf("Hello World\n"); ?>
Code Sample: Hello World Program in PHP
<html> <head> <title>Hello World PHP Page</title> </head> <body> <h1>A Simple PHP Script</h1> <?php printf("<p>Hello World</p>"); ?> </body> </html>
Code Sample: Hello World Program in PHP embedded with HTML
What can PHP do?
Anything. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynamic page content, or send and receive cookies. But PHP can do much more.
There are three main areas where PHP scripts are used.
- Server-side scripting. This is the most traditional and main target field for PHP. You need three things to make this work: the PHP parser (CGI or server module), a web server and a web browser. You need to run the web server, with a connected PHP installation. You can access the PHP program output with a web browser, viewing the PHP page through the server. All these can run on your home machine if you are just experimenting with PHP programming.
- Command line scripting. You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks.
- Writing desktop applications. PHP is probably not the very best language to create a desktop application with a graphical user interface, but if you know PHP very well, and would like to use some advanced PHP features in your client-side applications you can also use PHP-GTK to write such programs. You also have the ability to write cross-platform applications this way. PHP-GTK is an extension to PHP, not available in the main distribution.