Module 6 - Introduction To Hypertext Preprocessor
Module 6 - Introduction To Hypertext Preprocessor
Overview
Do you remember how websites used to be very static and boring to look at not too long ago? In contrast,
today’s websites are more dynamic, interactive and easier to use overall. One of the things that made
this possible is a programming language called PHP.
PHP, or Hypertext Preprocessor, is a popular scripting language used to create the attractive, user-
friendly and interactive Web pages that we see today. PHP is open-source which means that it is well-
documented and can easily be downloaded for free from the Web.
Objectives
Introduction to PHP
Why PHP?
The best thing about using PHP is extremely easy for a newcomer, and also it has many advanced
features for a professional programmer.
Learning PHP is very easy, and it runs efficiently on the server-side.
PHP works on many operating systems such as Linux, Windows, Mac OS X.
PHP is FREE to download from the official PHP resource: php.net
PHP supports many databases like MySQL, MS SQL, Oracle, Sybase, PostgreSQL, and many
others.
PHP can dynamically generate HTML, PDF, Flash, Text, CSV, XML and many others.
Coding in PHP is easy and fast, so it takes less time to build an application.
Many good PHP frameworks like Zend, Codeigniter, and Laravel are available for PHP.
Many web hosting options are available at a fair price for PHP.
With PHP, code deployment is very easy.
Substantial PHP community support, and many tutorials and sample programs are available
online.
To run PHP code, you need the following three software on your local machine:
Start where?
To execute PHP code, you need access to a web server in which PHP is running.
Install Apache and PHP or Install WAMP, LAMP, MAMP or XAMPP according to your OS.
After installation, you need to run and start the Apache Server and PHP from your program list,
and then type http://localhost in your web browser for a start working.
In the Apache installation folder, you will find the www folder, where you can save PHP files, and
you can also create your project folder inside the www folder.
Find a web hosting plan with Apache, PHP, and MySQL support and Run your PHP scripts on
your Web host.
You can also use runphponline.com interface to interpret your PHP code online.
Note: You can watch this link in YouTube to know how to install wamp server.
(https://www.youtube.com/watch?v=xoMggz9-n6E&feature=emb_imp_woyt)
1. Windows Notepad
2. Vi or Emacs on Linux
3. TextEdit on Mac OS X
Note: Commercially, Adobe Dreamweaver, NetBeans, and Sublime Text are used to edit PHP code.
PHP Basics
This lesson gives you a fundamental understanding of PHP files, its configuration and How PHP works.
PHP file will have a .php or .php3 extension; you also have the option of changing the default file
extension using the web server's configuration file.
A .php file should have all the PHP code within <?php (opening tag) and ?> (closing tag), then It
will be considered as PHP language code and parsed accordingly. It is also useful when you want
to write PHP code with HTML.
PHP can be embedded inside HTML but in a slightly different fashion. In between the HTML tags
where ever you want to make use of PHP code, you will have to use <?php /*PHP code*/ ?> and
PHP will parse this.
All PHP statements must end with a ; (semicolon) which is also called statement terminator. It is
essential because semicolon tells the PHP interpreter to read this line as a single statement, and
without this, you will see error messages while interpreting the code.
The web server only sends those files to an interpreter, whose file extension is .php, all other files
such as .html, .htm and others are not sent to PHP interpreter even if they contain PHP codes
inside them.
Once the file is sent to the PHP interpreter, it starts finding all of the opening and closing PHP tags
and then process the PHP code inside these tags.
PHP interpreter also checks that whether there is a database connection or not, if it finds database
connection then it will send or retrieve data from the database.
PHP scripts are interpreted on the web server, and outcome (HTML) is sent back to the client
machine.
As shown in the picture, PHP
interprets the page and
primarily communicates with
the file system, database, and
email server, and then delivers
a web page to the web server
to return to the browser.
PHP Syntax
This lesson has a detailed description of PHP Syntax. It's essential for you to before proceeding to learn
more advanced lessons in PHP.
PHP Tags
PHP is designed to work with HTML, so you can easily write and embed PHP code with HTML. A PHP
code block begins with <?php tag and ends with ?> tag.
To run this PHP program, you need to write above code in any text editor and save it to your web server
under www directory with a .php extension.
Once it's done, start the server and go to localhost and type in the path of your file, i.e.,
http://localhost/HelloWorld.php
Comments in PHP
The PHP interpreter ignores the comment block; it can be used anywhere in the program to add
info about program or code block, which can be helpful for the programmer to understand the
code easily in the feature.
In PHP, we can use // or # to make a single-line comment, and /* and */ to make a large
comment block.
PHP functions are similar to other programming languages. A function is a piece of code which takes one
more input in the form of parameter and does some processing and returns a value.
You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP
gives you option to create your own functions as well.
In fact, you hardly need to create your own PHP function because there are already more than 1000 of
built-in library functions created for different area and you just need to call them according to your
requirement.
Note that while creating a function its name should start with keyword function and all the PHP code should
be put inside { and } braces as shown in the following example below.
<html>
<head>
<title>Writing PHP Function</title>
</head>
<body>
<?php
/* Defining a PHP Function */
function writeMessage() {
echo "You are really a nice
person, Have a nice time!";
}
</body>
</html>
<html>
<head>
<title>Writing PHP Function with
Parameters</title>
</head>
<body>
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>
</body>
</html>
<html>
<head>
<title>Dynamic Function Calls</title>
</head>
<body>
<?php
function sayHello() {
echo "Hello<br />";
}
$function_holder = "sayHello";
$function_holder();
?>
</body>
</html>
Dynamic Websites
The Websites provide the functionalities that can use to store, update, retrieve, and delete the data
in a database.
Below example shows the form with some specific actions by using post method.
What is Validation?
Validation means check the input submitted by the user. There are two types of validation are
available in PHP. They are as follows:
Server-Side Validation − After submitted by data, the data has sent to a server and perform
validation checks in server machine.
Valid URL
Below code shows validation of URL
$website = input($_POST["site"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
Above syntax will verify whether a given URL is valid or not. It should allow some keywords as https, ftp,
www, a-z, 0-9,..etc..
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid format and please re-enter valid email";
}
Above syntax will verify whether given Email address is well-formed or not. If it is not, it will show an error
message.
<tr>
<td>E-mail: </td>
<td><input type = "text" name = "email">
<span class = "error">* <?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td>Time:</td>
<td> <input type = "text" name = "website">
<span class = "error"><?php echo $websiteErr;?></span>
</td>
</tr>
<tr>
<td>Classes:</td>
<td> <textarea name = "comment" rows = "5" cols =
"40"></textarea></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type = "radio" name = "gender" value =
"female">Female
<input type = "radio" name = "gender" value =
"male">Male
<span class = "error">* <?php echo $genderErr;?></span>
</td>
</tr>
<td>
<input type = "submit" name = "submit" value = "Submit">
</td>
</table>
<?php
echo "<h2>Your given values are as:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $class = $course = $subject = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}else {
$email = test_input($_POST["email"]);
if (empty($_POST["course"])) {
$course = "";
}else {
$course = test_input($_POST["course"]);
}
if (empty($_POST["class"])) {
$class = "";
}else {
$class = test_input($_POST["class"]);
}
if (empty($_POST["subject"])) {
$subjectErr = "You must select 1 or more";
}else {
$subject = $_POST["subject"];
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<tr>
<td>E-mail: </td>
<td><input type = "text" name = "email">
<span class = "error">* <?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td>Time:</td>
<td> <input type = "text" name = "course">
<span class = "error"><?php echo $websiteErr;?></span>
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type = "radio" name = "gender" value =
"female">Female
<input type = "radio" name = "gender" value =
"male">Male
<span class = "error">* <?php echo $genderErr;?></span>
</td>
</tr>
<tr>
<td>Select:</td>
<td>
<select name = "subject[]" size = "4" multiple>
<option value = "Android">Android</option>
<option value = "Java">Java</option>
<option value = "C#">C#</option>
<option value = "Data Base">Data Base</option>
<option value = "Hadoop">Hadoop</option>
<option value = "VB script">VB script</option>
</select>
</td>
</tr>
<tr>
<td>Agree</td>
<td><input type = "checkbox" name = "checked" value =
"1"></td>
<?php if(!isset($_POST['checked'])){ ?>
<span class = "error">* <?php echo "You must agree to
terms";?></span>
<?php } ?>
</tr>
<tr>
<td>
<input type = "submit" name = "submit" value =
"Submit">
</td>
</tr>
<?php
echo "<h2>Your given values are as :</h2>";
echo ("<p>Your name is $name</p>");
echo ("<p> your email address is $email</p>");
echo ("<p>Your class time at $course</p>");
echo ("<p>your class info $class </p>");
echo ("<p>your gender is $gender</p>");
</body>
</html>