Lab 9 Introduction To Web Programming and PHP
Lab 9 Introduction To Web Programming and PHP
Table of Contents
1. Introduction
4. Concept Map
4.1 PHP Variables
4.2 PHP Form Handling
4.3 PHP Data Types
4.4. PHP Operators
4.4 PHP Conditional Statements
4.5 Loops
5. Procedure& Tools
5.1 Tools
5.2 Setting-up and Setting up XAMPP (MySQL, Apache) [Expected time
= 5mins]
5.3 Walkthrough Task [Expected time = 30mins]
6. Evaluation Criteria
7. Practice Tasks
7.1 Task 01
7.2 Task 02
7.3 Task 03
7.4 Outcome
7.5 Testing
9. Evaluation Criteria
Easy to use.
It has a very simple syntax unlike other languages such as Perl or C. Rather than writing lots of
code to create a webpage, we create HTML documents and embed simple PHP codes into them.
It has multi-platform support. It supports all major operating systems. Moreover, the syntax is
consistent among different platforms. You can create PHP codes in Windows and easily switch
to UNIX. All PHP commands are enclosed within special start and end tags:
<?php
…PHP code…
?>
For instance, if the PHP code is embedded into an HTML document, the PHP interpreter reads
and executes only the PHP code enclosed within the start and end tags.
To learn about the PHP language and its usage with HTML
4. Concept Map
To see how PHP works with HTML, create the code below using notepad.
<html>
<head><basefont face= "Arial "></head>
<body>
<h2>Q: This creature can change color to blend in with its surroundings. What is its name?</h2>
<?php
//print output
echo "<h2><i>A: Chameleon </i></h2>";
?>
</body>
</html>
A PHP script consists of one or more statements, with each statement ending in a semicolon (ex:
echo ‘<h2><i>A: Chameleon </i></h2>’; ).
For greater readability, you should add comments to your code. Comments can be written after
“//” (ex: //print output).
Save this script as question.php and browse to it. View the source code of the web page you have
created by clicking “Source” in the “View” tab in Internet Explorer. You will see:
<html>
<head><basefont face="Arial"></head>
<body>
<h2>Q: This creature can change color to blend in with its surroundings. What is its name?</h2>
<h2><i>A: Chameleon </i></h2>
</body>
</html>
When the code is executed, PHP converted the code inside the “<?php” and “?>” tags to regular
HTML code! Everything outside these tags is ignored by PHP and returned as is.
4.1 PHP Variables
A variable in PHP can be used to store both numeric and nonnumeric data.
1) Every variable has a name, which is preceded by a dollar ($) symbol.
2) Variable names are case sensitive and they must begin with a letter or underscore
character.
We can replace the PHP code above with:
<?php
//define variable
$answer = 'A: Chameleon';
//print output
Echo "<h2><i>$answer</i></h2>";
?>
This will produce the same result as before.
1) To assign a value to a variable, use the equality (=) symbol (ex: $answer = ‘A:
Chameleon’ ;).
2) To use a variable value in your script, call the variable by its name. PHP will substitute
its value when the code is executed (ex: Echo “<h2><i>$answer</i></h2>” ;).
4.2 PHP Form Handling
You can add interactivity to your web site using FORMS. A form enables your users to submit
inputs to your web site. Create the HTML document below to get user input (save it as
getinput.html). Then we will manipulate this input using a PHP script.
<html>
<head></head>
<body>
<form action= "message.php" method= "post">
Enter your message: <input type= "text" name= "msg" size= "30">
<input type="submit" value="Send">
</form>
</body>
</html>
1) “action” attribute specifies the name of the script that will process the information
entered into the form. Here, the input entered into the form will be sent to message.php.
2) The value of the input entered is stored in the variable named msg.
Now create the script that will process the input and save it as message.php:
<?php
// retrieve form data in a variable
$input = $_POST['msg'];
// print it
Echo "You said: <i>$input</i>";
?>
3) To access the value of a form variable, use its name inside $_POST (ex: $_POST['msg']).
Now, run the getinput.html and enter some data into the form (“hello”) and submit it.
Message.php should read it and display it back to you (“You said: hello).
4.3 PHP Data Types
There are four basic data types in PHP. PHP can automatically determine the variable type by the
context in which it is being used.
Data Type Description Example
Boolean Specifies a true or false value. $auth = true;
Integer Integers like -98, 2000. $age = 28;
Floating-point Fractional numbers such as $temp = 76;
12.8 or 3.149391
String Sequence of characters. May be $name = ‘Ismail’;
enclosed in either double quotes
or single quotes.
1) PHP has its own set of rules about which operators have precedence over others
(Operators on the same line have the same level of precedence):
“!”
“*”, “/”, “%”
“+”, “-”, “.”
“<”, “<=”, “>”, “>=”
“= =”, “!=”, “= = =”, “!= =”
“&&”
“||”
4.4 PHP Conditional Statements
A conditional statement enables you to test whether a specific condition is true or false, and to
perform different actions on the basis of the test result. We will use the if( ) statement to create
conditional statements:
<?php
if (conditional test)
{
do this;
}
else
{
do this;
}
?>
1) If the conditional expression after “if” evaluates to true, all PHP code within the
following curly brackets is executed. If not, the code coming after the “else” is executed.
2) The “else” part of the above code can be removed. In that case, if the conditional
expression is false, the code within the curly braces is skipped and the lines following the
“if” construct are executed.
<?php
if ($temp >= 100)
{
echo 'Very hot!';
}
else
{
echo 'Within tolerable limits';
}
?>
PHP also provides you with a way of handling multiple possibilities:
<?php
if ($country == 'UK')
{ $capital = 'London'; }
elseif ($country == 'US')
{ $capital = 'Washington'; }
elseif ($country == 'FR')
{ $capital = 'Paris'; }
else
{
$capital = 'unknown';
}
?>
4.5 Loops
A loop is a control structure that enables you to repeat the same set of commands over and over
again. The actual number of repetitions may be dependent on a number you specify, or on the
fulfillment of a certain condition.
The simplest loop in PHP is the while loop With this loop type, so long as the conditional
expression specified evaluates to true, the loop will continue to execute. When the condition is
false, the loop will be broken and the statements following it will be executed.
<?php
$num = 11; // define number and limits for multiplication tables
$upperLimit = 10;
$lowerLimit = 1;
while ($lowerLimit <= $upperLimit) // loop and multiply to create table
{
echo "$num x $lowerLimit =" . ($num*$lowerLimit);
$lowerLimit++;
}
?>
This script uses a while loop to create a multiplication table for the given table. It starts with “11
x 1 = 11” and continues until “11 x 10 = 110”.
1) “$lowerLimit++;” does the same job as “$lowerLimit = $lowerLimit + 1;”
Department of Computer Page 7
Science, MAJU, 2023
Lab 09 Introduction to Web Programming and PHP
If the loop condition evaluates as false on the first iteration of the loop, the loop will never be
executed. However, sometimes you might need to execute a set of commands at least once.
Regardless of how the conditional expression evaluates. For such situations, PHP offers the do-
while loop. The construction of the do-while() loop is such that the statements within the loop
are executed first, and the condition to be tested is checked after.
<?php
do
{
do this
} while (condition is true)
?>
Let’s now revise the previous PHP script so that it runs at least once, regardless of how the
conditional expression evaluates the first time.
<?php
// define number and limits for multiplication tables
$num = 11;
$upperLimit = 10;
$lowerLimit = 12; // loop and multiply to create table
do
{
echo "$num x $lowerLimit =" . ($num*$lowerLimit);
$lowerLimit++;
} while ($lowerLimit <= $upperLimit)
?>
Both while and do-while loops continue to iterate for so long as the specified conditional
expression remains true. But there often arises a need to execute a certain set of statements a
fixed number of times. We use the for() loop for this purpose.
<?php
for (initialize counter; conditional test; update counter)
{
do this
}
?>
The for loop uses a counter that is initialized to a numeric value, and keeps track of the number
of times the loop is executed. Before each execution of the loop, a conditional statement is
tested. If it evaluates to true, the loop will execute once more and the counter will be
incremented by 1 (or more). If it evaluates to false, the loop will be broken and the lines
following it will be executed instead.
To see how this loop can be used, create the following script, which lists all the numbers
between 2 and 100:
<?php
for ($x = 2; $x <=100; $x++)
{
echo "$x";
}
?>
5. Procedure& Tools
5.1 Tools
In this section tools installation and setup is defined.
Desktop Computer
Microsoft Windows XP operating system
Internet Browser(Internet Explorer, Mozilla Firefox or Google Chrome etc)
Notepad
5.2 Setting-up and Setting up XAMPP (MySQL, Apache) [Expected time = 5mins]
Figure 4: Logout
13) Open google Mozilla Firefox and write URL : http://localhost:<Your’s XAMP Apache port
> /WalkthroughTask/index.php
Figure 5: Output
6. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
7. Practice Tasks
7.1 Task 01
Create a website that asks a simple question and retrieves the viewer’s answer from an array of
Q/As. Display messages on the screen depending on the answer:
Otherwise, display a message like “Your answer was -------. The correct answer is ------.
7.2 Task 02
Use the project created above in 7.1 and Put the Questions and answers into a session variable.
7.3 Task 03
Show all Question Answers from Session in tabular form, also add edit and delete button in next
column for edit and delete the Question
7.4 Outcome
After completing this lab, students will be able to perform some functionality using PHP and also
understand the concept of web application development using PHP.
7.5 Testing
Test Cases for Practice Task-1 and Task-2
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student
has finished the complete/partial task(s).
Table 3: Evaluation of the Lab
Sr. No. Task No Description Marks
http://www.htmlcodetutorial.com/