Saint Paul University Philippines
DIT 305 : E-Commerce
Professor: Dr. Amando P. Singun, Jr.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Uploading files to a web page
Watermarking images
Generating thumbnails from
larger images
Displaying & updating
information dynamically
Using a database to display
and store information
Making websites searchable
And much more . . .
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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
What is PHP?
- Server-Side language
(versus JavaScript, CSS)
How Safe is PHP?
What is important is to
understand the basic principle of
PHP safety: always check user input
before processing it.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
How to test PHP pages on your
computer?
- A Web Server (Apache or IIS)
- PHP
- MySQL (if working w/
database)
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
WAMP means
- Windows Apache
- MySQL
- PHP
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and 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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
How to check if php is working on
Windows?
Step 1. Open Notepad
Step 2. Type these codes:
<?php phpinfo( ); ?>
Step 3. Save as phptest2.php
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Every PHP Page must have the ff:
- Opening & closing php tags
<?php phpinfo( ); ?>
Or
<?php
phpinfo( );
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
A typical PHP page will use some
or all of the following elements:
- Variables
- Arrays
- Conditional Statements
- Loops
- Functions
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and 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
4. Variable names are case-sensitive:
$startYear and $startyear are not
the same.
Examples:
$startYear, $thisYear
$start_year, $this_year
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Exercise: Is the variable VALID or
INVALID?
1) $lowestPrice VALID
2) $_product VALID
3) $5thCustomer INVALID
4) $invoice% INVALID
5) $discount_ VALID
6) $PRICE VALID
7) $phone number INVALID
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Assigning Values to Variables
Syntax:
$variable=value;
Ending Commands w/ a
semicolon
<?php echo phpversion( ); ?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Commenting Scripts
- Single-line comments
// this is a comment
Instead of the //, you can also
use the hash or pound sign #.
###############
### 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*/
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Exercise:
Write the codes to declare a variable
named txt and to display the text -
Hello World.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Answer:
<?php
$txt=Hello World;
echo $txt;
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Note:
PHP ignores
Concatenation
white space in
code.
$firstName = Amando';
$lastName = Singun';
echo $firstName . $lastName;
// displays AmandoSingun
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
// displays Amando Singun
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
$course[prof'] = Professors name is
Dr. Amando.;
Rule:
Quotes must always be in matching
pairs
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
How to solve this problem?
2 Ways:
1. Use double quotes if the text
includes any apostrophes.
2. Precede apostrophes with a
backslash (this is known as
escaping).
Rewrite:
$course[prof'] = Professors
name is Dr. Amando.;
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Evaluate:
$profName = Professors name
Dr. Amando ;
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Answer(s):
$profName = Professor\s name
Dr. Amando ;
$profName = Professors name
\Dr. Amando\ ;
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Exercise:
Concatenate the following w/ a space
between:
The value of txt1 is Hello World!
The value of txt2 is What a nice
day!
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Laboratory Exercise:
Concatenation
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
<?php
$firstname="AMANDO";
$middlename="PIMENTEL";
$lastname="SINGUN";
$space=" ";
$fullname=
$firstname.$space.$middlename.$space.$lastname;
echo $fullname;
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Answer:
<?php
$txt1=Hello World;
$txt2=What a nice day!;
echo $txt1. .$txt2;
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
//Dealing with arithmetic
<?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/>";
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Run the ©
Source Codes: <?php
$startYear=2007;
$thisYear=date(Y);
if($startYear==$thisYear){
echo $startYear; }
else{
echo {$startYear}-{$thisYear};
}
?>
AMANDO SINGUN, JR.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Forms
Output: form1.html
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Output: process1.php
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Forms
Output: form2.html
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Forms
Output: process2.php
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Forms
Output: form3.php
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Forms
After clicking SEND button, it displays the output below
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Note:
Decision
Making Decisions
making in PHP
uses
conditional
statements.
if statement only if a specified
condition is true
ifelse statement if one condition
is true and another is false
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
ifelseifelse statement to select
one of several blocks of code to be
executed
switch statement to select one of
many blocks of code to be
executed
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Note:
Conditional if ($status == 'administrator') {
statements
checks whether // send to admin page
the condition
being
}
tested equates to
true.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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!
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=="Wed");
echo "Have a nice weekend!";
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
<?php
$day=date("D");
if($day== Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Using variables to represent
changing values
Exercise: Copyright Code
2017 AMANDO SINGUN, JR.
After 5 years, it has to be auto-updated as
2017-2022 AMANDO SINGUN, JR.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Each crayon costs Forms with Conditional Statements
0.500
Go back to your previous work
Each Clip costs
0.200 form1.php
Each Colored Paper
costs 0.100
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Each crayon costs process1.php
0.500
Each Clip costs
0.200
Each Colored Paper
costs 0.100
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
<html> Laboratory Exercises *Practical
<head> Windows Apache, MySQL, and PHP
<title>formA</title>
</head>
<body bgcolor="pink">
<form action="process1.php" method="POST">
Name: <input type="text" name="name">
Items: <select name="items">
<option>SELECT HERE</option>
<option>Crayons</option>
<option>Clips</option>
<option>Colored Papers</option>
</select>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Quantity: <Input type="text" name="quantity">
Windows
<input type="Submit" Apache, MySQL, and PHP
name="submit"
value="Order">
</form>
</body>
</html>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
<?php Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
$name=$_POST['name'];
$items=$_POST['items'];
$quantity=$_POST['quantity'];
echo "<font color='red'>";
echo "Welcome ".$name."<br>";
echo "You have bought ".$quantity." of
".$items.".<br>";
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
if($items=='Crayons') Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
{
$price=$quantity * 0.500;
echo "The total price is ".$price;
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.
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>";
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
NetPay.php
Condition NET PAY is computed as
If the employee Basic 3% of Basic Salary
Salary is more than or equal
to 1000
If the employee Basic 2% of Basic Salary
Salary is more than or equal
to 500
If the employee Basic 1% of Basic Salary
Salary is more than or equal
to 100
If the employee Basic Very Low Earner!
Salary is less than 100
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
You can test your work
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
dowhile loops through a block of
code once, and then repeats the
loop as long as a specified
condition is true
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
foreach loops through a block of
code for each element in an
array
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
for Loop
for (initial statement; condition for
the loop; end-loop condition)
{
// code to be executed;
// code to be executed;
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Output: Guess the output of the following:
1 2 3 4 5 6 7 8 9 10
for ($count=1; $count<11;
$count=$count+1)
{
echo $count;
echo ;
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Output: Guess the output of the following:
1 2 3 4 5 6 7 8 9 10
for ($count=1; $count<11;
$count=$count+1)
{
echo $count;
echo ;
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Write the for loop statement:
Display even numbers from 2 to 10
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
while Loop
while (condition)
{
// code to be executed;
// code to be executed;
}
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Guess the output of the following:
<?php
$i=1;
while ($i<=5)
{
echo The number is .$i.<br />;
$i++;
}
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Output: Guess the output of the following:
The number is 1
The number is 2 <?php
The number is 3
The number is 4 $i=1; 654321 True
False It will stop there.
The number is 5
while ($i<=5)
{ 54321
echo The number is .$i.<br />;
$i++;
65432
}
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
dowhile Loop
do
{
// code to be executed;
// code to be executed;
}
while (condition)
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Guess the output of the following:
<?php
$i=1;
do
{
$i++;
echo The number is .$i.<br />;
}
while ($i<=5);
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Output: Guess the output of the following:
The number is 2
The number is 3 <?php
The number is 4
The number is 5 $i=1;
The number is 6
do
{ 3+1
5+1 = 45326 It will stop increment here.
1+1
2+1
4+1
$i++; 32654
echo The number is .$i.<br />;
}
while ($i<=5);
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Output: Exercise: Find the output.
1 2 3 4 5 6 7 8 9 10
$count = 1;
while ($count < 11)
{
echo $count;
echo ;
$count = $count + 1;
}
Now, convert the codes into do-while loop.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
More Exercises
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Write the for loop statement:
Display 10 9 8 7 6 5 4 3 2 1
Convert this to While and
Do-While loop statements.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Write the for loop statement:
Display 2 4 8
Convert this to While and
Do-While loop statements.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Write the for loop statement:
Display 3 6 9 12 15 18 21 24 27 30
Convert this to While and
Do-While loop statements.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Write the for loop statement:
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Write the for loop statement:
Convert this to While and
Do-While loop statements.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Find the Output:
<?php
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
Find the Output:
<?php
$a=3; $b=4;
do{
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo $a.$b;
}
while($a > $b)
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Answer:
4334
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Note: Guess the output of the following:
if ($counter % 2 ==
0) <?php
// Go through each table
for ($table=1; $table<13; $table++)
{
echo "<p><b>The " . $table . " Times Table</b>\n";
// Produce 12 lines for each table
for ($counter=1; $counter<13; $counter++)
{
$answer = $table * $counter;
// Is this an even-number counter?
if ($counter % 2 == 0)
// Yes, so print this line in bold
echo "<br><b>$counter x $table = " .
"$answer</b>";
else
// No, so print this in normal face
echo "<br>$counter x $table = $answer";
}
}
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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');
$mailSent = mail($to, $subject,
$message);
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Displaying PHP Output
$name = Amando';
echo $name; // displays Amando
echo 2010; // displays 2010
echo Amando'; // displays Amando
echo(Amando'); // displays Amando
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
PHP Error Messages
Where it went wrong?
Severity of Error What went wrong?
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Categories of Error (in
descending order of
importance)
1) Fatal error
2) Parse error
3) Warning
4) Notice
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
$fruit = '2 apples';
$veg = ' 2 carrots';
echo $fruit + $veg;
// displays 4
$fruit = '2 apples';
$veg = ' and 2 carrots';
echo $fruit + $veg;
// displays 2
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Given: Arithmetic Operators in PHP
$x = 20;
$y = 10; Addition + $x + $y = 30
$z = 2; Subtraction - $x - $y = 10
Multiplication * $x * $y = 200
Division / $x / $y = 2
Modulo division % $x % $z = 0
Increment (adds 1) ++ $x++ = 21
Decrement (subtracts 1) -- $y-- = 9
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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Evaluate:
$result = 2 * 2 + 3 * 3 + 5;
=4+3*3+5
=4+9+5
Left-to-Right Rule
= 13 + 5
= 18
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Laboratory Exercise:
Arithmetic Operators
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
<?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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Comparison Operators
== 5==8 FALSE
> 5>8 TRUE
< 5<8 TRUE
<> 5 <> 8 TRUE
!= 5 != 8 TRUE
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
>= 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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
|| or
Given: Evaluate:
x=6 (x<10 || y>9)
y=3 T F
TRUE
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
! not
Given: Evaluate:
x=6 !(x==y)
y=3 F
TRUE
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Note: Escape Sequences Inside
The escape
sequences listed Double Quotes
in on the right
side, with the
\" Double quote
exception of \\, \n New line
work only in \r Carriage return
double-quoted \t Tab
strings. \\ Backslash
\$ Dollar sign
\{ Opening curly brace
\} Closing curly brace
\[ Opening square bracket
\] Closing square bracket
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
Laboratory Exercise:
Form between HTML and PHP
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, 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>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
</select>
  Quantity<input type="textbox"
name="quantity">
<input type="Submit">
</form>
</body>
</html>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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!";
?>
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
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
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
- Built-in superglobal Arrays
All begin with a dollar sign
followed by an underscore.
E.g. $_POST
$_GET
$_POST[address]=1600 Pennsylvania
Avenue;
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
- Built-in superglobal Arrays We Will
Use in this Course
1. $_POST: This contains values sent
through the post method. Example: send the
content of an online feedback form by email
to your inbox.
2. $_GET: This contains values sent
through a URL query string. Example: To
pass information to a database.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
Laboratory Exercises *Practical
Windows Apache, MySQL, and PHP
3. $_SERVER: This contains information
stored by the web server, such as filename,
pathname, hostname, etc.
4. $_FILES: This contains details of file
uploads.
5. $_SESSION: This stores information
that you want to preserve so that it's
available
to other pages. Example: Used to create a
simple login system.
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.
End of
Thank
Presentation
You
Thank You
Copyright 2017 AMANDO P. SINGUN. All Rights Reserved.