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,&quot;MSIE&quot;) ) { echo &quot;You are using Internet Explorer&quot;; } ?> 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(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;); foreach ($arr as $value) {echo “Number: &quot; . $value . &quot;<br />&quot;;} ?>
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,&quot;MSIE&quot;)) { ?> < p >You are using Internet Explorer</ p > < ?php } else { ?> < p >You are not using Internet Explorer</ p > < ?php } ?> Instead of using a n echo statement to print something, we jumped out of PHP mode . N ote that the logical flow of the PHP remain s 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[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ ?> <form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> <p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> <p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> <input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </form> <?php } else{ $sn = $_REQUEST['surname']; echo &quot;<p>Thank you, $sn.</p>&quot;; $addr = $_REQUEST['address']; echo &quot;<p> We will write to you at $addr .</p>&quot;; } ?> </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. */ ?>

Control Structures In Php 2

  • 1.
  • 2.
    Control Structures –if statements if ( $a > $b ) echo &quot;a is bigger than b&quot;; if ( $a > $b ) { print &quot;a is bigger than b&quot;; $b = $a; } if ( $a > $b ) { print &quot;a is bigger than b&quot;; } else { print &quot;a is NOT bigger than b&quot;; } if ( $a > $b ) { print &quot;a is bigger than b&quot;; } elseif ( $a == $b ) { print &quot;a is equal to b&quot;; } else { print &quot;a is smaller than b“; }
  • 3.
    Example usage Example<html> <head><title>Your browser</title></head> <body> <h1>Your Browser</h1> <p> <?php if( strstr($HTTP_USER_AGENT,&quot;MSIE&quot;) ) { echo &quot;You are using Internet Explorer&quot;; } ?> 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
  • 4.
    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
  • 5.
    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 ) ;
  • 6.
    Control constructs --for These are just like their counterparts in C for ($i = 1; $i <= 10; $i++) { print $i;}
  • 7.
    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(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;); foreach ($arr as $value) {echo “Number: &quot; . $value . &quot;<br />&quot;;} ?>
  • 8.
    Jumping in andout 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,&quot;MSIE&quot;)) { ?> < p >You are using Internet Explorer</ p > < ?php } else { ?> < p >You are not using Internet Explorer</ p > < ?php } ?> Instead of using a n echo statement to print something, we jumped out of PHP mode . N ote that the logical flow of the PHP remain s intact Only one of the HTML blocks will be sent to the user .
  • 9.
    A FORM andits handler in one <html> <head> <title>Application Handler</title> </head> <body> <?php if (! $_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ ?> <form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> <p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> <p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> <input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </form> <?php } else{ $sn = $_REQUEST['surname']; echo &quot;<p>Thank you, $sn.</p>&quot;; $addr = $_REQUEST['address']; echo &quot;<p> We will write to you at $addr .</p>&quot;; } ?> </body> </html>
  • 10.
    Finding out aboutyour 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
  • 11.
    Adding Comments toa 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
  • 12.
    Adding Comments toa 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
  • 13.
    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. */ ?>

Editor's Notes