Control Structures In Php 2

kfeighery 2,690 views 13 slides Feb 01, 2009
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

No description available for this slideshow.


Slide Content

Control Structures in PHP

Control Structures – if
statements
if ($a > $b)
echo "a is bigger than b";
if ($a > $b)
{print "a is bigger than b";
$b = $a;}
if ($a > $b)
{print "a is bigger than b"; }
else {print "a is NOT bigger than b"; }
if ($a > $b)
{print "a is bigger than b"; }
elseif ($a == $b)
{print "a is equal to b"; }
else {print "a is smaller than b“; }

Example usage
Example
<html>
<head><title>Your browser</title></head>
<body>
<h1>Your Browser</h1>
<p>
<?php
if( strstr($HTTP_USER_AGENT,"MSIE") )
{ echo "You are using Internet Explorer"; }
?>
to view this page.
</p>
</body>
</html>
strstr is a function which checks if its 2
nd

argument is a substring of its 1
st

Control constructs -- looping
In PHP we have the following looping
statements:
while - loops through a block of code if and as
long as a specified condition is true
do...while - loops through a block of code once,
and then repeats the loop as long as a special
condition is true
for - loops through a block of code a specified
number of times
foreach - loops through a block of code for each
element in an array

Control constructs -- while
These are just like their counterparts in C
$i = 1;
while ( $i <= 10 )
{ echo $i++; }
$i = 0;
do { print $i;} while ($i>0);

Control constructs -- for
These are just like their counterparts in C
for ($i = 1; $i <= 10; $i++)
{ print $i;}

Control constructs -- foreach
These are similar their counterparts in Perl
foreach(array_expression as $value)
statement
foreach(array_expression as $key => $value)
statement
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{echo “Number: " . $value . "<br />";}
?>

Jumping in and out of PHP
mode
We can jump in and out of PHP mode even in the
middle of a PHP block:
<?php
if(strstr($HTTP_USER_AGENT,"MSIE"))
{ ?> <p>You are using Internet Explorer</p> <?php }
else { ?> <p>You are not using Internet Explorer</p> <?php }
?>
Instead of using an echo statement to print
something, we jumped out of PHP mode.
Note that the logical flow of the PHP remains intact
Only one of the HTML blocks will be sent to the user.

A FORM and its handler in one
<html>
<head>
<title>Application Handler</title>
</head>
<body>
<?php
if (! $_POST["surname"] or !$_POST["address"]){
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>Your surname: <input type="text" name="surname"></p>
<p>Your address: <input type="text" name="address"></p>
<input button type="submit" value= "Please send me the brochure.">
</form>
<?php
}
else{
$sn = $_REQUEST['surname'];
echo "<p>Thank you, $sn.</p>";
$addr = $_REQUEST['address'];
echo "<p> We will write to you at $addr .</p>";
}
?>
</body>
</html>

Finding out about your PHP
environment
One of the many pre-defined PHP functions is
phpinfo()
<html>
<body>
<h1>Your PHP Environment</h1>
<?php phpinfo(); ?>
</body>
</html>
In what follows, notice that mySQL support is
enabled

Adding Comments to a PHP
Script
Comments are nonprinting lines
placed in code such as:
The name of the script
Your name and the date you created the
program
Notes to yourself
Instructions to future programmers who
might need to modify your work

Adding Comments to a PHP
Script (continued)
Line comments hide a single line of
code
Add // or # before the text
Choose and stick with version
Block comments hide multiple lines of
code
Add /* to the first line of code
And */ after the last character in the code

Example Comments
<?php
/*
This line is part of the block comment.
This line is also part of the block comment.
*/
echo (“<h1>Comments Example</h1>”); // Line comments
can follow
code statements
// This line comment takes up an entire line.
# This is another way of creating a line comment.
/* This is another way of creating
a block comment. */
?>