Lecture - 9 - PHP - Overview
Lecture - 9 - PHP - Overview
Content
Introduction to PHP
Freely available
The PHP group provides complete source code free of
charge
Similar syntax to C, Pearl
Works with many operating systems
Can be deployed on many web servers
Interacts with lots of databases
It is supported by many providers of webhosting
Websites using PHP
Significant examles
User-facing portion of Facebook
Wikipedia (MediaWiki)
Yahoo!
MyYearbook
What do You Need to work with PHP?
Comments
// ,# comment
/* comment */
Writing of the plain text
Echo “text”
print “text”
Example 1- Helloworld.php
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Variables in PHP
Each variable starts with $ symbol
<?php
$txt = "HelloWorld!";
$number = 16;
?>
Variable types
Numerical
Integer – positive as well as negative, including 0
Float – real numbers, 14 digits accuracy
Logical
Boolean - True x False, not case sensitive
Alphabetical
String – set of characters
Working with variables
Settype($var, “integer”)
allows you to set variable according to your wish
Gettype()
write the type of variable
(.)
Connects 2 variables of string type
strlen()
finds the length of a string
Example 2
<?php
$var = "Bob";
$Var = "Joe";
echo $var ;//outputs Bob
echo $Var;//outputs Joe
$4site = "not yet";// invalid; starts with a number
$_4site = "not yet";// valid; starts with an underscore
?>
Example 3 – String variables
<?php <?php
$txt="Hello World"; $txt1="Hello World.";
echo $txt; ?> $txt2=“We are in 2008.";
echo $txt1 . " " . $txt2;
/*note use of ‘.’ for
concatenation*/
?>
Assignment operators
x+=y x=x+y
Logical operators
&& = and
|| = or
At least one of condition is fulfilled
! = not
xor
Exactly one statement is evaluated as true
Conditional Statements
If/ else
After each statement stands (;)
If more than one command should be executed, use curly braces { }
Switch / break
Used for choosing one possibility from multiple cases
Switch ($var )
{
case : “x” : echo “good”;
break;
default : echo “wrong input” ;
}
Example 4
<html><body>
<?php
switch ($examGrade)
{ case “A”: echo “ Very Good";
break;
case “F”: echo “ Failure";
break;
default: echo “ Average";
break;
}
?></body></html>
Arrays in PHP
Numeric array
Each element of array has its ID number (first 0!!)
$names = array(“Petr”, “Joe”);
$names[0] = "Petr";
Associative Arrays
Each element is assigned its value
$ages = array("Peter"=>32, "Joe"=>34);
$ages['Peter'] = "32";
PHP Looping
while
loops repeat until final condition is reached
$i =1;
while ($i<=10)
{
echo $i;
$i++;
}
do...while
kind of reversed while function
Do { code to be executed;}
While(final condition);
PHP Looping
for
Repeats the specific part of code so many times we choose
for ($i=0;$i<count($modules);$i++)
{
echo (“module $i is modules[$i] <br/>”);
}
?>
Example 7
<?php
/* printing an associative array */
$modules[CSE 1041]=“ Web Technologies”;
$modules[CSE 2242]=“Computer Graphics”;
$modules[CSE 2046]=“Multimedia Authoring”;
<?php
echo
"<TR>
<TD>".$i."</TD><TD>".$i*$i."</TD></TR>\n";
?>
PHP Functions
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
Exercise
Write the HTML code for FormInput.htm having a layout as in
the diagram below:
txt_id
txt_pwd
txt_email
txt_module
rb_classSize
cb_delivery
cb_lab
txt_others
Method POST,
processForm.php
Exercise contd
Write the PHP code for the processForm.php to get the display as
in the diagram below, based on the input form FormInput.htm
in the previous slide
Including other files
<html> <body>
<?php include(“myPage.php"); ?>
<h1>Web Technologies Course</h1>
…
</body>
</html>