0% found this document useful (0 votes)
92 views

Advanced Web Based Application Development: Introduction To Server-Side Scripting Using PHP

This document provides an introduction to server-side scripting using PHP. It discusses PHP syntax, creating and saving PHP scripts, executing PHP scripts using XAMPP, and setting up a local development environment with XAMPP on Windows. The goal is to help readers understand server-side scripting concepts and write simple PHP scripts that can be executed locally for development purposes.

Uploaded by

Luvuno Nzole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Advanced Web Based Application Development: Introduction To Server-Side Scripting Using PHP

This document provides an introduction to server-side scripting using PHP. It discusses PHP syntax, creating and saving PHP scripts, executing PHP scripts using XAMPP, and setting up a local development environment with XAMPP on Windows. The goal is to help readers understand server-side scripting concepts and write simple PHP scripts that can be executed locally for development purposes.

Uploaded by

Luvuno Nzole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Advanced Web Based Application

Development

Lecture 2

Introduction to Server-side Scripting Using PHP

Dr. Obuhuma James


Description

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

By the end of this topic, you will be able to:

▪ Understand the concepts of server-side scripting.


▪ Write, save, and execute a simple standalone PHP script.
▪ Setup XAMPP as an execution environment for PHP scripts.
▪ Embed PHP scripts into HTML, save and execute such files.

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:

1. Manage input and output.


2. Give room for manipulation of values in a symbolic manner that is independent of a
particular computer stores information internally.
3. Perform arithmetic operations on numbers.
4. Perform operations on characters and strings of characters.
5. Facilitate decision-making based on comparing values.
6. Perform repetitive calculations.
PHP Script Syntax

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]:

1. The standard PHP script delimiter


<?php
Statements;
?>
2. The <script> element
<script language=”php”>
Statements;
</script>
3. Short PHP script delimiter
<?
Statements;
?>
4. ASP-style script delimiter
<%
Statements;
%>
The choice of style to use is sometimes influenced by preferences of the programmer.
However, it is worth noting that the short PHP delimiter and ASP-style delimiter options may
be enabled and disabled from the php.ini configuration file. On the other hand, the <script>
element option must have the language attribute set to value “php”. This unit will use the
standard PHP script delimiter style in all its examples for consistency purposes.

Creating and Saving PHP Scripts

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:

echo “Hello World”;


echo ‘Hello World’;
echo (“Hello World”);
echo (‘Hello World’);
3. Each statement in PHP must be terminated by a semicolon.
4. PHP is case insensitive. Hence, the word echo, Echo, and ECHO are interpreted the
same.

Supposing we want to save the file with a filename “hello”, then we should save it as
“hello.php”.

Displaying Script Results

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.

Setting up the Webserver

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.

Figure 1. Antivirus Status

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.

Figure 3. XAMPP Setup Wizard Launch

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.

Figure 4. Choice of Installation Components

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

Figure 6. XAMPP Language

Figure 7. Learn More About Bitnami for XAMPP


Figure 8. Setup Ready to Install XAMPP

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.

Figure 9. Installation in Progress


Figure 10. Completion of the XAMPP Setup Wizard

7. Click “Finish” to launch the XAMPP Control Panel.

Figure 11. XAMPP Control Panel

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.

Running PHP Scripts on the Browser

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

Figure 15. The Working Folder for the Course “uuooi”

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 18. Accessing the uuooi Directory

Figure 19. Writing a “Hello Word” Script in a Text Editor


Figure 20. Saving the “Hello World” Script in the uuooi Folder

Figure 21. Accessing the uuooi Directory on the Browser with Files in it

Figure 22. The Executed hello.php Script on the Browser

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.

//line one commented out


//line two commented out
….
//line n commented out

2. Multiple line comments

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.

/* line one commented out.


line two commented out.
….
line n commented out. */

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.

Figure 23. Adding Comments in PHP Scripts


Figure 24. Executed Script Omitting 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.

Embedding PHP into HTML

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.

Figure 25. Embedding PHP Scripts in 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 Code Blocks

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.

Figure 27. Embedding Multiple PHP Code Blocks in HTML


Figure 28. Output of Embedding Multiple PHP Code Blocks in HTML

Incorporating HTML Elements in PHP

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.

Figure 29. Incorporating HTML Elements in PHP


Figure 30. Output of Incorporating HTML Elements in PHP

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

1. Describe the concepts behind scripting.


2. Describe the components of XAMPP that make it critical for server-side scripting.
3. Describe the process of installing XAMPP on a Windows OS.
4. Demonstrate how HTML tags can be embedded into PHP scripts.
5. Demonstrate how PHP scripts can be embedded into HTML web pages.

Learning Resources

Core Textbooks

1. Brooks, D.R., 2017. Programming in HTML and PHP. Switzerland: Springer.


2. Gosselin, D., Kokoska, D. and Easterbrooks, R., 2010. PHP Programming with MySQL:
The Web Technologies Series. Cengage Learning.

Other Resources

3. https://www.w3schools.com/php/DEFAULT.asp
4. https://www.tutorialspoint.com/php/index.htm

References

[1] Brooks, D.R., 2017. Programming in HTML and PHP. Switzerland: Springer.


[2] Gosselin, D., Kokoska, D. and Easterbrooks, R., 2010. PHP Programming with MySQL:
The Web Technologies Series. Cengage Learning.

You might also like