Advanced Web Based Application Development: Introduction To Server-Side Scripting Using PHP
Advanced Web Based Application Development: Introduction To Server-Side Scripting Using PHP
Development
Lecture 2
This topic aims at introducing you to server-side scripting with a focus on the Hypertext
Preprocessor (PHP) for web applications. It gives a brief introduction to PHP, the syntax for
PHP files, how to write, save, and execute PHP files. The topic further outlines how PHP
scripts can be embedded into HTML files with clarity on how execution occurs in such cases.
Learning Outcomes
Introduction to PHP
PHP is a server-side scripting language that has gained fame in the current world of web-
based applications, spanning from 1994. As an open-source server-side programming
language, PHP tries to fill the gap that static HTML pages fail to address. This is achieved
through the introduction of dynamism in web pages. According to [3], PHP provides a way
to solve computational problems by breaking them into discrete steps that can be carried
out in a top-down approach. It is hence capable of performing the following tasks:
A script in this case refers to a set of PHP statements aimed at accomplishing some given
task. PHP scripts are written within code blocks that may be achieved in four different
formats. These are [2]:
PHP scripts can be written using any basic text editor like notepad or any specialised text
editor like notepad++, sublime text, Dreamweaver, among others. The scripts should be
saved in a file with a .php extension. For instance, consider a simple script that will display
“Hello World”. The following code could be used.
<?php
Echo “Hello World”;
?>
Note that:
1. Either echo or print can be used to print the message “Hello World”. Both are output
functions.
2. The statement “Hello World” in this case is a literal string that must be displayed as
is. Such literal strings can be enclosed in single or double quotation marks. Brackets
could also enclose the quotation marks and the literal strings. For instance, each of
the following approaches is valid:
Supposing we want to save the file with a filename “hello”, then we should save it as
“hello.php”.
As the server executes scripts, any data that is meant for display on a webpage is created by
an echo or print statement. It is the choice of the programmer to use whichever statement
for the purpose. This is clearly explained in the previous section in the “Hello World”
example.
Case Sensitivity
PHP is case insensitive. This means that code can be written in lower case, upper case or
even a mix of lower and upper case. However, for better and ease of reading, it is good
practice to use one case throughout the script.
Executing PHP Scripts
To execute a PHP script, a webserver must be set up with the appropriate services running
on the server. The PHP script files must then be saved in a specific location on the
webserver and accessed appropriately on the browser.
PHP scripts execute in a webserver. The most common webservers are Apache and Internet
Information Services (IIS) webservers. Apache is a software foundation that develops and
provides open-source software that is meant to run webservers. HTTP is Apache’s primary
product that is the most popular HTTP server in use today. IIS on the other hand is a
software pack developed by Microsoft to provide their Windows operating system with the
ability to host internet services. IIS is second only to HTTP as the most used HTTP server in
the world.
The Apache webserver is free, hence, very popular. This is very advantageous to those who
are just trying out web publishing and are still unsure about it. Apache is part of the XAMPP
software suite. XAMPP is a collection of open-source software suited for web publishing
requirements from the OS down to the scripting language. Apart from being free, the open-
source community is always a suitable source of support for users who have the time and
patience to seek for answers to tough questions and support.
Conversely, IIS is a Microsoft product, hence, designed to only run on the Microsoft
Windows Operating System. IIS comes as a free product that rides on the Microsoft
Windows Operating Systems license. Using IIS comes with the advantage that most people
are already familiar with the Windows Operating System, making IIS much easier to learn for
Windows users. IIS is also supported by the .NET Framework released by Microsoft. This
automatically means guaranteed support by Microsoft. It is worth noting that ASPX scripts, a
competitor of PHP scripts, is exclusive to IIS.
PHP scripts can run on either Apache or IIS webservers, provided that the PHP execution
engine is installed and configured correctly. This course will use the Apache webserver that
comes as part of the XAMPP package. The initial X in XAMPP represents the operating
systems that XAMPP operates on i.e., Linux, Windows, and Mac OS X; A represents the
Apache webserver; M is MySQL (or MariaDB), as relational Database Management System
(DBMS) in XAMPP; while the double Ps represent Perl and PHP as scripting languages in
XAMPP. Additionally, XAMPP contains more useful tools, which vary depending on the
operating system. These tools include Mercury, a mail server; phpMyAdmin, a database
administration environment, Webalizer, a web analytics software solution; OpenSSL, an end
to end Security Socket Layer; FileZilla or ProFTPd, File Transfer Protocol servers; and the
XAMPP Control Panel, for monitoring and regulating the operation of other components of
XAMPP.
XAMPP is the most popular PHP development environment that is completely free and easy
to install. It is an Apache distribution that currently contains MariaDB, PHP, and Perl. Visit
Apache friends website, https://www.apachefriends.org to download and install the XAMPP
version of your choice, based on your operating system. However, all demonstrations for
this unit will be on the Windows Operating System. On the same website, you will find a
YouTube video with steps on how to install and configure XAMPP for Windows operating
system. The same video could be accessed via https://youtu.be/h6DEDm7C37A. The
following is a snapshot of the set of the steps followed to help kickstart your installation
after downloading the executable installation file.
1. Run the executable file by double-clicking on it. This results in the following window.
Click “Yes” to proceed.
2. A warning pops up relating to User Account Controls (UAC). Click “Ok” to proceed.
Figure 2. User Account Control Status
3. The installation wizard will now be ready to start the installation process. Click “Next” to
proceed.
4. When prompted to select components to install, leave the default selections or be sure
to select MySQL, PHP and phpMyAdmin, in case they are not checked. Click “Next” to
proceed.
5. Select the installation folder, language, and whether you want to learn more from
Bitnami respectively as you click “Next” to proceed. This will take you to a point where
the installation will get ready. Also, click “Next” to start the installation.
Figure 5. XAMPP Installation Folder
6. The installation will start, with a progress indicator showing the estimated percentage of
the installation. This may take a couple of minutes until the installation is completed.
8. The XAMPP Control Panel will show you various services available at your disposal. You
can start, stop, configure, and view logs for any of the services. For the sake of this
course, we are only interested in Apache and MySql. Be sure to start the two services.
This will highlight them in colour green, an indicator that they are started and running
correctly. Any other colour will be an indicator that the services are not running or are
being interfered with by other services that may be using the same ports. You will also
be able to see the service status with a timestamp at the bottom for your information.
Figure 12. XAMPP Control Panel with Apache and MySQL Services Started
Up to this point, all will be set up and ready for execution of PHP scripts. The XAMPP Control
Panel icon will remain on the system tray for ease of access when needed.
In reference to XAMPP installed on a Windows OS, based on the installation location folder
that was selected during installation, a subfolder called htdocs will be part of the subfolders
in the main installation folder. All PHP scripts must be loaded in this folder. The following
screenshots, labeled as Figure 13 – 15, show examples of the folders and where PHP scripts
should be saved.
Figure 13. The Set of Default Files and Folders in the XAMPP Folder
Figure 14. Default Contents of the htdocs Folder
PHP script files must be run directly from a client, in this case, a web browser. This means
that we cannot open the files directly by double-clicking on them or right-clicking and
selecting open as we do for other files. This is because server-side scripts must be executed
from the server first before the results are sent to the client for display. Figure 16 – 22
outlines steps on how to run PHP scripts from the web browser as a client.
Figure 16. Option 1: Opening the Webserver from the Browser (Using http://localhost)
Figure 17. Option 2: Opening the Webserver from the Browser (Using http://127.0.0.1)
Figure 21. Accessing the uuooi Directory on the Browser with Files in it
The example demonstrating how to write, save, and execute a PHP script is a clear indicator
that by now the webserver is well set up and configured.
PHP Comments
Comments are normally crucial for the developer. They help in documenting the code, and
for facilitation of troubleshooting. Commenting on a line or segment of code makes the
interpreter skip the line(s) from execution. Hence, the regions with comments do not
appear on the output displayed on the browser. There are two ways to include comments in
PHP code.
1. Single line comments
Single line comments are applied to one line at a time. The line being commented precedes
with double forward slashes “//”. For instance, the following is an example of how three
lines could be commented.
Multiple line comments are applied to more than one line at a time, or better say a block of
code. The lines being commented precedes with a single forward slash followed by an
asterisk “/*” and closed with an asterisk followed by a single forward slash “*/”. For
instance, the following is an example of how three lines could be commented together.
The following example shows a PHP script and its output. The script is demonstrating
scenarios for both single line and multiple line comments. The script also has some regions
without comments.
Notice the fact that all the lines with comments have been ignored by the server and the
client as evident in the output on the browser.
PHP codes, just like JavaScript codes, can be embedded into HTML documents. As a result,
both PHP and JavaScript are sometimes referred to as embedded languages. The codes are
normally typed directly into HTML webpages as separate sections embracing the syntax
rules for the respective scripting language. The following example shows a PHP script
embedded into HTML.
Notice the two code segments i.e. an HTML segment and a PHP script segment. It is worth
pointing out that the moment a PHP script is embedded in an HTML document, then the file
must be saved as .php. This is because the server must execute the PHP segment first before
the page gets to the client for execution of the HTML segments. The output for the above
example code is as follows.
Figure 26. Output of Embedding PHP in HTML
It is worth noting that the PHP script code segments can be positioned before the beginning
of the HTML segment, within the body of the HTML segment, or at the very end of the
HTML segment.
Multiple PHP script segments can also be embedded in one HTML webpage as outlined in
the following example. This will only work if each segment has an independent opening and
closing as per PHP syntax rules.
HTML tags can also be embedded directly into PHP scripts in cases where specific effects
have to be applied to text or any other objects being defined in PHP scripts. All the HTML
and PHP rules must be followed appropriately in such cases. This is as shown in the example
below.
Take note of how the selected examples of HTML tags have been embedded into PHP
scripts. The same approach could be applied to any other HTML tags.
Summary
The topic has laid a foundation to PHP by outlining concepts of server-side scripting; how to
write, save, and execute a simple standalone PHP script; the process of setting up XAMPP as
an execution environment for PHP scripts; and how to embed PHP scripts into HTML and
HTML tags into PHP scripts. From this point onwards, we should be good to proceed into
deep PHP programming for web-based applications.
Check Points
Learning Resources
Core Textbooks
Other Resources
3. https://www.w3schools.com/php/DEFAULT.asp
4. https://www.tutorialspoint.com/php/index.htm
References