Laboratory Exercises
Laboratory Exercises
Laboratory Exercises
What is PHP?
- Hypertext Preprocessor
- A Scripting Language
Sending feedback
from your website
directly to your
mailbox
Sending email with
attachments
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
What is PHP?
- Platform-neutral
- FOSS
- Started in 1995 as Personal
Home Page
- Developed further, adding
extensive support for OOP
in PHP 5
What is PHP?
- Server-Side language
(versus JavaScript, CSS)
WAMP means
- Windows Apache
- MySQL
- PHP
Note:
Always SAVE How to check php version?
your work in the
server root
folder.
Step 1. Open Notepad
Step 2. Type these codes:
<?php echo phpversion( ); ?>
Step 3. Save as phptest1.php
Naming Variables
Rules:
1. Variables always begin with a
dollar sign ($).
2. The first character after the
dollar sign cannot be a number.
3. No spaces or punctuation are
allowed, except for the
underscore (_).
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Examples:
$startYear, $thisYear
$start_year, $this_year
Commenting Scripts
- Single-line comments
// this is a comment
###############
### Menu section ###
###############
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
- Multiline comments
/* this is a multiline comment
and is ignored by the php
engine*/
Exercise:
Write the codes to declare a variable
named txt and to display the text -
Hello World.
Answer:
<?php
$txt=Hello World;
echo $txt;
?>
Note:
PHP ignores
Concatenation
white space in
code.
$firstName = Amando';
$lastName = Singun';
// displays AmandoSingun
Note:
You must either $firstName = Amando';
include a space
in one of the
$lastName = Singun';
strings or insert
the space as a
string echo $firstName.' '.$lastName;
in its own right
Note:
String means
When to Use Quotes
a set of
characters in $txt=Hello World;
text. $txt=Hello World;
$startYear = 2006;
$course['title'] = E-Commerce';
Rules:
Numbers: No quotes
Text: Requires quotes
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Rule:
Quotes must always be in matching
pairs
Note:
An apostrophe $course[prof'] = Professors name is
inside a single-
quoted string Dr. Amando;
confuses the
PHP engine.
$course[prof'] = Professors name
is Dr. Amando ;
Rule:
Quotes must always be in matching
pairs
Evaluate:
$profName = Professors name
Dr. Amando ;
Answer(s):
$profName = Professor\s name
Dr. Amando ;
Exercise:
Concatenate the following w/ a space
between:
The value of txt1 is Hello World!
The value of txt2 is What a nice
day!
Laboratory Exercise:
Concatenation
<?php
$firstname="AMANDO";
$middlename="PIMENTEL";
$lastname="SINGUN";
$space=" ";
$fullname=
$firstname.$space.$middlename.$space.$lastname;
echo $fullname;
?>
Answer:
<?php
$txt1=Hello World;
$txt2=What a nice day!;
Forms
Output: form1.html
Output: process1.php
Forms
Output: form2.html
Forms
Output: process2.php
Forms
Output: form3.php
Forms
After clicking SEND button, it displays the output below
Note:
Decision
Making Decisions
making in PHP
uses
conditional
statements.
if statement only if a specified
condition is true
Note:
Conditional if ($status == 'administrator') {
statements
checks whether // send to admin page
the condition
being
}
tested equates to
true.
Note:
Conditional
statements
if ($status == 'administrator') {
checks whether // send to admin page
the condition
being }
tested equates to else {
true.
// refuse entry to admin area
}
Note: If you
want a default
action to be
if (condition is true) {
invoked, use // code to be executed if
else.
condition is true
}
else {
// default code to run if condition
is false
}
Note: If you
want more if (condition is true) {
alternatives/opti // code to be executed if first condition is
ons, use elseif. true
}
elseif (second condition is true) {
// code to be executed if first condition fails
// but second condition is true
else {
// default code to run if both conditions are
false
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Exercise:
If the current day is Sunday,
display Have a nice Sunday!
<?php
$day=date("D");
if($day=="Wed");
echo "Have a nice weekend!";
?>
Exercise:
If the current day is Sunday, the
output is Have a nice Sunday!,
otherwise the output is Have a
nice day!.
What conditional statement are you
going to use? Why? Give the codes.
<?php
$day=date("D");
if($day== Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
Note: Observe:
If more than one
line should be <?php
executed if a $day=date("D");
condition is
true/false, the
lines should be if($day==Sun")
enclosed within {
curly brackets.
echo "Hello! <br />";
echo "Have a nice Sunday!";
echo See you next meeting!";
}
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Exercise:
If the current day is Thursday, the
output is Happy holiday! and
if the current day is Friday the
output is Happy Friday!.
Otherwise, the output is Enjoy
your working day!
What conditional statement are you
going to use? Why? Give the codes.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
<?php
$day=date('D');
if($day=="Thu")
echo "Happy holiday!";
elseif($day=="Fri")
echo "Happy Friday!";
else
echo "Enjoy your working day!";
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
echo "<br>";
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
elseif($items=='Clips') Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
{
echo "The total price is ".$quantity *
0.200."<br>";
if($price < 1)
echo " baiza.";
elseif($price >= 1 && $price == 1)
echo " rial. ";
else
echo " rials.";
echo "<br>";
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
else Laboratory Exercises *Practical
{ Windows Apache, MySQL, and PHP
$price=$quantity * 0.100;
echo "The total price is ".$price."<br>";
if($price < 1)
echo " baiza.";
elseif($price >= 1 && $price == 1)
echo " rial. ";
else
echo " rials.";
echo "<br>";
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
echo "</font>"; Windows Apache, MySQL, and PHP
echo "<br>";
echo "<a href='form1.html'>"."Back"."</a>";
?>
Note: Loops
Loops are huge
time-savers for loops through a block of code
because they within a specified number of
perform the
same task over times
and over again
with little codes.
while loops through a block of code
while a specified condition is true
for Loop
while Loop
while (condition)
{
// code to be executed;
// code to be executed;
}
dowhile Loop
do
{
// code to be executed;
// code to be executed;
}
while (condition)
More Exercises
Answer:
for($i=3;$i>=1;$i-=2)
{
for($x=$i; $x>=1; $x--)
{
echo "*";
}
echo "</br>";
}
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Answer:
4334
for($i=7;$i>=1;$i-=2)
{
for($j=$i;$j>=1;$j--)
{
echo "*";
}
echo "</br>";
}
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
foreach Loop
Note:
Functions are
Functions
easy to identify
in PHP code
because theyre $thisYear = date('Y');
always followed //passing an argument to a function.
by a pair of
parentheses
$thisMonth = date('M');
$name = Amando';
echo $name; // displays Amando
Note:
PHP is what is
Data Types in PHP
known as a
weakly typed 1) Integer
language.
2) Floating-point number
3) String
4) Boolean
5) Array
6) Object
7) Resource
8) NULL
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Note:
PHP is what is Given:
known as a
weakly typed
$x = 20; $x + $y = 30
language. $y = 10; $x - $y = 10
$z = 2; $x * $y = 200
$x / $y = 2
$x % $z = 0
$x++ = 21
$y-- = 9
Note:
Use parentheses 26 % 5 = 1
all the time to
group the parts
10 % 2 = 0
of a calculation
that you want
to make sure are
performed as a Order of Calculation
single unit.
( ) M-D-A-S
Evaluate:
$result = 2 * 2 + 3 * 3 + 5;
=4+3*3+5
=4+9+5
Left-to-Right Rule
= 13 + 5
= 18
Laboratory Exercise:
Arithmetic Operators
<?php
$add=1+2;
$sub=2-1;
$mul=1*2;
$div=2/2;
$mod=5 % 2;
echo " ARITHMETIC OPERATORS<br/>";
echo "1 plus 2 equals ".$add."<br/>";
echo "2 minus 1 equals ".$sub."<br/>";
echo "1 times 2 equals ".$mul."<br/>";
echo "2 divided 2 equals ".$div."<br/>";
echo "5 modulu 2 equals ".$mod."<br/>";
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Assignment Operators
= $a=$b $a = $b
+= $a += $b $a = $a + $b
-= $a -= $b $a = $a - $b
*= $a *= $b $a = $a * $b
/= $a /= $b $a = $a / $b
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
%= $a %= $b $a = $a % $b
.= $a .= $b $a = $a . $b
Comparison Operators
== 5==8 FALSE
!= 5 != 8 TRUE
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Logical Operators
&& and
Given: Evaluate:
x=6 (x<10 && y>1)
y=3 T T
TRUE
|| or
Given: Evaluate:
x=6 (x<10 || y>9)
y=3 T F
TRUE
! not
Given: Evaluate:
x=6 !(x==y)
y=3 F
TRUE
Note:
Technically How PHP Treats Variables
speaking, using Inside Strings
double quotes
when you dont $name = Amando';
need to process // Single quotes: $name is treated as literal
any variables
is inefficient.
text
echo 'Hello, $name';
$name = Amando';
// Double quotes: $name is processed
echo "Hello, $name";
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Laboratory Exercise:
Form between HTML and PHP
Main.html
<html>
<head>
<title>Ordering SYstem</title>
</head>
<body>
WELCOME TO APS ART SUPPLIES SHOP <br>
<form action="process.php" method="POST">
<select name="item">
<option>Paint</option>
<option>Brush</option>
<option>Eraser</option>
</select>
</select>
  Quantity<input type="textbox"
name="quantity">
<input type="Submit">
</form>
</body>
</html>
Process.php
<?php
$item= $_POST['item'];
$quantity= $_POST['quantity'];
echo "You ordered ".$quantity. " of ".$item." .<br/>";
echo "Thank you for shopping at APS Art Supplies
Shop!";
?>
Note:
The indexed
Array
array key is not - Array Elements
enclosed in
quotes.
The first item in the array, oil, is
referred to as $shoppingList[0],
not $shoppingList[1].
And although there are five
items, the last one (grapes) is
$shoppingList[4].
- Indexed Array
Note:
The associative $course['title'] = E-Commerce';
array key is
enclosed in
$course[code'] = BAEB2203';
quotes (single or $course[passingMark'] = C-';
double, it $course[lecturer'] = Mr. Amando';
doesnt matter)
- Associative Array
$_POST[address]=1600 Pennsylvania
Avenue;
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP