PHP is a popular open-source scripting language. It is widely deployed as a web-server side scripting language. PHP scripts are embedded within HTML pages.
PHP offers excellent support for many databases like MySQL, Oracle etc. This makes it even more relevant for web-servers since web-applications, typically, use databases to store data.
This module on PHP presents an overview of PHP elements like variables, loops, conditional expressions, functions, arrays, strings, objects, error-handling, and interaction with MySQL and HTML. A good understanding of these concepts would allow the reader to develop high-quality, robust, and scalable PHP applications.
Before we proceed further, let us understand the steps to run a PHP program. Typically, a PHP code is embedded in an HTML page using tags "<?php" and "?>". Any code that sits between these two tags executes in the PHP context. Such a PHP code can be placed anywhere in an HTML page and can be placed any number of times as needed in an HTML page. Since PHP is an interpreter based language, there is no need to compile these programs!
We provide below a simple PHP "Hello World!" program. As mentioned above, we embed this program in an HTML file (let us call this file, "hello.php"). This program uses a PHP function "echo" to print the string "Hello World!"; when run, the output would get displayed on the browser. The line that contains the echo function is terminated by a semicolon and is often referred to as a statement.
<!doctype html> <html> <body> <?php echo "Hello World! <br>"; ?> </body> </html>
We need to follow a handful of steps to run this program.
First, we need to store this file at a location that is accessible to the web server (in Fedora/Red Hat Linux, the default location for web server is "/var/www/html"). Second, we need to ensure that the web server itself is running (in Fedora/Red Hat Linux, we can use "service httpd status" to see if the web server is running). If the web server is not running, then we need to start it (in Fedora/Red Hat Linux, we can use "service httpd start" to start the web server). Lastly, we need to load this file from a browser ("http://localhost/hello.php").
When we load this file, the web server would run the PHP code. Here is the output:
Running a PHP program using a browser is certainly the most popular method. Nevertheless, it is not the only method! Another method is to run it from the command line. For example, we can run the above program as "php hello.php" from the directory that contains "hello.php". If we run it from other directory, then we need to provide the full path for "hello.php" file. Since, we are not running this from HTML context, we can safely remove the HTML tags. With these changes, the updated program appears simpler:
<?php echo "Hello World! \n"; ?>
Since we are running it from the console, we have replaced "<br>" tag with the newline character "\n". Here is the output when we run this program from the console.
$ php hello.php Hello World!
For all examples in this PHP module, we will run them as part of an HTML page. Also, for the sake of readability, we will show text output from the webpage as text instead of embedding the snapshot of HTML page.
For PHP program to work, we need to ensure that PHP is installed on the web-server. As an example, to install Linux on Fedora, we can use the yum command as in "yum install php". Once PHP is successfully installed, then we need to restart the webserver (as in "service httpd restart").
For additional information on installation of PHP, we refer the reader to PHP website provided by PHP Group (www.php.net).
To check if PHP is installed on the webserver, a simple way is to embed the following in the HTML file: "<?php phpinfo(); ?>". If PHP is installed, then we would see various information about PHP. Else, we would see no output on the screen. Here is how we can embed phpinfo() in an HTML file.
<!doctype html> <html> <body> <?php phpinfo(); ?> </body> </html>
We provide below the output for "<?php phpinfo(); ?>". For the sake of readability, we don't show the complete output.