(COSC111AdvancedProgramming)
The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers
to create dynamic content that interacts with databases.
The Origin of PHP
This software, originally known as Personal Home Pages, was developed in 1997 and is currently
being used in over 10 million domains. Before PHP, developers used CGI (Common Gateway Interface)
scripts for interacting with users, querying databases, etc. However, since CGI applications are separate,
stand-alone programs, scalability is missing because multiple CGI applications running concurrently can
quickly consume all available memory. To avoid this problem, PHP was developed using a three layer
architecture. The first layer is the client also known as the browser. The second layer is the server
(including both the web server and PHP interpreter). The third layer is the database system. Using this
approach, it avoids the forking of external programs found in CGI applications which can quickly consume
all available memory.
With PHP you write HTML script with embedded code defined with special start and end tags.
The embedded code is executed on the server and then sent to your browser. What is displayed in the
browser is the result from the code's execution. This is very different from other scripting languages like
Perl and C where code is written to output HTML commands. While similar to JavaScript, the main
difference with PHP is the code is executed on the servers, whereas with JavaScript the code is executed
on the client-side. With JavaScript it is possible to determine the underlying code, whereas with PHP the
underlying code is hidden from the user.
How PHP works?
Every time a visitor goes to your site to read your content, a request is made that is sent to a host
server. The PHP programming language receives that request, makes a call to the MySQL database,
obtains the requested information from the database, and then presents the requested information to your
visitor via his web browser.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 2
(COSC111AdvancedProgramming)
Content refers to the data stored in the MySQL database which includes blog posts, pages,
comments, links, and options. The theme (or design) you choose to use for your site isn't part of the
database content. Themes files are considered the presentation layer and are essential to user friendly
website display. Theme files are part of the file system and aren't stored in the database. So creating and
keeping a backup of any theme files that you're currently using are good ideas.
Hardware and Software requirements and installation
install PHP
Pros
Cons
Strength of PHP
Supports database connectivity. PHP can access over 20 different databases including MySql,
Oracle, and MS Access.
Supports sessions. PHP can generate unique session IDs. The ID follows the user during a single
session on a web site.
Eliminates client configuration problems. With PHP there is no need to worry if the client has the
appropriate software installed, since the application is executed on the server.
Reduces development time. Even a newcomer can begin developing PHP applications in hours.
Yet PHP contains many advanced features for professional programmers.
Maintains source code security. The user does not see your source code as they do with
JavaScript.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 3
(COSC111AdvancedProgramming)
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 4
(COSC111AdvancedProgramming)
PHP Variables
Example:
$X=2;
$Y=10;
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case sensitive ($y and $Y are two different variables)
Activity no.1
Topic: Creating and using variables
Direction:
1. Echo the following statement to the browser:
Twinkle, Twinkle little star.
2. Create two variables, one for the word Twinkle and one for the word star. Echo the statement
to the browser, this time substituting the variables for the relevant words. Change the value of
each variable to whatever you like, and echo the statement a third time. Remember to include
code to show your statements on different lines.
Sample Output:
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 5
(COSC111AdvancedProgramming)
Twinkle, Twinkle little star.
Twinkle, Twinkle little star.
Thunder, Thunder little elephant.
Sample script:
<html>
<head>
<title>Twinkle Twinkle</title>
</head>
<body>
<?php
echo"Twinkle, Twinkle little star. <br/>";
//Note that html tags can be included along with the text to be echoed.
$twinkle="Twinkle";
$star="star";
//This will print out exactly the same as the first echo statement.
echo"$twinkle, $twinkle little $star.<br/>";
$twinkle="Thunder";
$star="elephant";
/*This one will be different. Exactly what prints will depend
on what valued you assigned to the variables.*/
echo"$twinkle, $twinkle little $star.";
?>
</body>
</html>
PHP Operators
Operator
Name
Description
Example
Result
x+y
Addition
Sum of x and y
2+2
x-y
Subtraction
Difference of x and y
5-2
x*y
Multiplication
Product of x and y
5*2
10
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 6
(COSC111AdvancedProgramming)
x/y
Division
Quotient of x and y
15 / 5
x%y
Modulus
Remainder of x divided by y
5%2
10 % 8
10 % 2
1
2
0
-x
Negation
Opposite of x
-2
a.b
Concatenation
"Hi" . "Ha"
HiHa
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value
of the expression on the right. That is, the value of "$x = 5" is 5.
Assignment
Same as...
Description
x=y
x=y
The left operand gets set to the value of the expression on the right
x += y
x=x+y
Addition
x -= y
x=x-y
Subtraction
x *= y
x=x*y
Multiplication
x /= y
x=x/y
Division
x %= y
x=x%y
Modulus
a .= b
a=a.b
Operator
Name
Description
++ x
Pre-increment
x ++
Post-increment
-- x
Pre-decrement
x --
Post-decrement
Name
Description
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
Example
T.Baylon
CvSUCCC(LecManual) 7
(COSC111AdvancedProgramming)
x == y
Equal
True if x is equal to y
x === y
Identical
5==="5" returns
false
x != y
Not equal
x <> y
Not equal
x !== y
Not identical
x>y
Greater than
x<y
Less than
x >= y
x <= y
Operator
Name
Description
Example
x and y
And
x=6
y=3
(x < 10 and y > 1) returns true
x or y
Or
x=6
y=3
(x==6 or y==5) returns true
x xor y
Xor
x=6
y=3
(x==6 xor y==3) returns false
x && y
And
x=6
y=3
(x < 10 && y > 1) returns true
x || y
Or
x=6
y=3
(x==5 || y==5) returns false
!x
Not
x=6
y=3
!(x==y) returns true
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 8
(COSC111AdvancedProgramming)
Operato Name
r
Description
x+y
Union
Union of x and y
x == y
Equality
x === y
Identity
True if x and y have the same key/value pairs in the same order and are of the
same type
x != y
Inequality
x <> y
Inequality
x !== y
Nonidentity
Activity no.2
Topic: Arithmetic Operations and Variables
Direction:
PHP includes all the standard arithmetic operators. For this PHP exercise, you will use them along with
variables to print equations to the browser.
1. In your script, create the following variables:
$x=10;
$y=7;
2. Write code to print out the following:
10 + 7 = 17
10 7 = 3
10 * 7 = 70
10 / 7 = 1.4285714285714
10 % 7 = 3
Use numbers only in the above variable assignments, not in the echo statements. You
will need a third variable as well.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 9
(COSC111AdvancedProgramming)
NOTE: Use numbers only in the above variable assignments, not in the echo statements. You will
need a third variable as well.
Sample script:
<html>
<head>
<title>Arithmetic Operators</title>
</head>
<body>
<?php
$x=10;
$y=7;
$result=$x+$y;
echo "$x + $y = $result<br/>";
$result=$x-$y;
echo "$x - $y = $result<br/>";
$result=$x*$y;
echo "$x * $y = $result<br/>";
$result=$x/$y;
echo "$x / $y = $result<br/>";
$result=$x%$y;
echo "$x % $y = $result<br/>";
?>
</body>
</html>
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 10
(COSC111AdvancedProgramming)
If (condition)
{
Code to be executed if the condition is true;
}
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 11
(COSC111AdvancedProgramming)
if...else statement - executes some code if a condition is true and another code if the condition is
false.
If (condition)
{
Code to be executed if the condition is true;
}
else
{
Code to be executed if the condition is false;
}
if (condition)
{
Code to be executed if the condition is true;
}
else if (condition)
{
Code to be executed if the condition is true;
}
else
{
Code to be executed if the condition is false;
}
switch statement - selects one of many blocks of code to be executed. The switch statement is
used to perform different actions based on different conditions.
Switch (n)
{
Case label
Code to
Break;
Case label
Code to
Break;
Default:
Code to
}
1:
be executed if n=label1;
2:
be executed if n=label2;
be executed if n is different from both label1 and label2;
For the switch statement, we have a single expression n (most often a variable), that is evaluated
once. The value of the expression is then compared with the values for each case in the structure. If there
is a match, the block of code associated with that case is executed. Use break to prevent the code from
running into the next case automatically. The default statement is used if no match is found.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 12
(COSC111AdvancedProgramming)
PHP Arrays
An array can hold many values under a single name, and you can access the values by referring
to an index number.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 13
(COSC111AdvancedProgramming)
The following example creates an indexed array named $cars, assigns three elements to it, and
then prints a text containing the array values:
Example:
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
or
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
The named keys can then be used in a script:
Example
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 14
(COSC111AdvancedProgramming)
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 15
(COSC111AdvancedProgramming)
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
?>
Sort Array in Descending Order, According to Value - arsort()
The following example sorts an associative array in descending order, according to the value:
Example
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);
?>
Sort Array in Descending Order, According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:
Example
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
?>
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 16
(COSC111AdvancedProgramming)
Syntax:
for (initialization; condition; increment)
{
code to be executed;
}
Sample Program:
<?php
$a = 0;
$b = 0;
for( $i=0; $i<5; $i++ )
{
$a += 10;
$b += 5;
}
echo ("At the end of the loop a=$a and b=$b" );
?>
Output:
At the end of the loop a=50 and b=25
WHILE Loop
- It loops through a block of code if and as long as a specified condition is true.it loops through a
block of code a specified number of times.
Syntax:
while (condition)
{
code to be executed;
}
Sample Program:
<?php
$i = 0;
$num = 50;
while( $i < 10)
{
$num--;
$i++;
}
echo (" i = $i and num = $num" );
?>
Output:
i = 10 and num = 40
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 17
(COSC111AdvancedProgramming)
DO WHILE Loop
- The do...while statement will execute a block of code at least once - it then will repeat the loop
as long as a condition is true.
Syntax:
do{ code to be executed;} while (condition);Sample Program:
<?php
$i = 11;
do
{
$i++;
}
while( $i < 10 );
Activity no.4
Topic: Simple Loops
Direction:
1.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 18
(COSC111AdvancedProgramming)
1
23456789
1.
2.
3.
4.
5.
6.
Item A
Item B
Item C
Item D
Item E
Item F
2. Create the 'abc' row with a while loop, the 'xyz' row with a do-while loop, and the last two sections
with for loops. Remember to include HTML and source code line breaks in your output. No arrays
allowed in this solution.
Sample script:
<html>
<head>
<title>Simple Loops</title>
</head>
<body>
<h2>Simple Loops</h2>
<?php
echo"<p>\n";
$counter= 1;
while($counter< 10){
echo'abc ';
$counter++;
}
echo"</p>\n";
echo"<p>\n";
$counter= 1;
do{
echo'xyz ';
$counter++;
}while($counter< 10) ;
echo"</p>\n";
for($x=1;$x<10;$x++){
echo"$x ";
}
//The space inside the "" is necessary to separate the numbers.
//Generate ordered list.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 19
(COSC111AdvancedProgramming)
echo"\n<ol>";
for($x='A';$x<'G';$x++){
echo"<li>Item $x</li>\n";
}
echo"\n</ol>";
//Note that letters may be used in the for loop in place of numbers.
?>
</body>
</html>
Activity no.5
Topic: Simple For Loops
Direction:
Loops are very useful in creating lists and tables.
1. Using a for loop, write a script that will send to the browser a list of squares for the numbers 1-12.
Use the format, 1 * 1 = 1 and be sure to include code to print each formula on a different line.
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 20
(COSC111AdvancedProgramming)
Sample output:
Squares for the Numbers 1-12
1*1=1
2*2=4
3*3=9
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49
8 * 8 = 64
9 * 9 = 81
10 * 10 = 100
11 * 11 = 121
12 * 12 = 144
Sample script:
<html>
<head>
<meta http-equiv="content-type"content="text/html;charset=iso-8859-1"/>
<title>Squares for the Numbers 1-12</title>
</head>
<body>
<h2>Squares for the Numbers 1-12</h2>
<?php
for($x=1;$x<=12;$x++){
$result=$x*$x;
echo"$x * $x = $result <br />\n";
}
?>
</body>
</html>
Activity no.6
Topic: Nested For Loops
Direction:
1. Use two for loops, one nested inside another. Create the following multiplication table:
1 2 3 4 5 6 7
2 4 6 8 1 1 1
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon
CvSUCCC(LecManual) 21
(COSC111AdvancedProgramming)
3 6 9
4 8
1
0
1
6
2
1
7
4
5
1
2
1
5
1
8
2
1
1
2
1
6
2
0
2
4
2
8
0
1
5
2
0
2
5
3
0
3
5
2
1
8
2
4
3
0
3
6
4
2
4
2
1
2
8
3
5
4
2
4
9
Sample script:
<html>
<head>
<title>Nested Loop Multiplication Table</title>
</head>
<body>
<h2>Nested Loop Multiplication Table</h2>
<?php
//Generate an HTML table
echo"<table border=\"1\">";
//Generate table data showing the numbers 1-7 multiplied by each other,
//starting with the rows.
for($row=1;$row<=7;$row++){
echo"<tr>\n";
//Generate each entry in the row to create the columns.
for($col=1;$col<=7;$col++){
//First, do the math.
$x=$col*$row;
//Then send the value to the table with the table data tags.
echo"<td>$x</td>\n";
}
echo"</tr>";
}
echo"</table>";
?>
</body>
</html>
Dept.ofICT&ComputerStudies1stSem.A.Y.20132014
T.Baylon