Control Structures In Php 2

1,319 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,319
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • <number>
  • <number>
  • <number>
  • <number>
  • <number>
  • <number>
  • <number>
  • <number>
  • Control Structures In Php 2

    1. 1. Control Structures in PHP
    2. 2. Control Structures – if statements <ul><li>if ( $a > $b ) </li></ul><ul><li>echo &quot;a is bigger than b&quot;; </li></ul><ul><li>if ( $a > $b ) </li></ul><ul><li>{ print &quot;a is bigger than b&quot;; </li></ul><ul><li>$b = $a; } </li></ul><ul><li>if ( $a > $b ) </li></ul><ul><li>{ print &quot;a is bigger than b&quot;; } </li></ul><ul><li>else { print &quot;a is NOT bigger than b&quot;; } </li></ul><ul><li>if ( $a > $b ) </li></ul><ul><li>{ print &quot;a is bigger than b&quot;; } </li></ul><ul><li>elseif ( $a == $b ) </li></ul><ul><li>{ print &quot;a is equal to b&quot;; } </li></ul><ul><li>else { print &quot;a is smaller than b“; } </li></ul>
    3. 3. Example usage <ul><li>Example </li></ul><ul><ul><ul><ul><li><html> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><head><title>Your browser</title></head> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><body> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><h1>Your Browser</h1> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><p> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><?php </li></ul></ul></ul></ul><ul><ul><ul><ul><li>if( strstr($HTTP_USER_AGENT,&quot;MSIE&quot;) ) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{ echo &quot;You are using Internet Explorer&quot;; } </li></ul></ul></ul></ul><ul><ul><ul><ul><li>?> </li></ul></ul></ul></ul><ul><ul><ul><ul><li>to view this page. </li></ul></ul></ul></ul><ul><ul><ul><ul><li></p> </li></ul></ul></ul></ul><ul><ul><ul><ul><li></body> </li></ul></ul></ul></ul><ul><ul><ul><ul><li></html> </li></ul></ul></ul></ul><ul><li>strstr is a function which checks if its 2 nd argument is a substring of its 1 st </li></ul>
    4. 4. Control constructs -- looping <ul><li>In PHP we have the following looping statements: </li></ul><ul><ul><li>while - loops through a block of code if and as long as a specified condition is true </li></ul></ul><ul><ul><li>do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true </li></ul></ul><ul><ul><li>for - loops through a block of code a specified number of times </li></ul></ul><ul><ul><li>foreach - loops through a block of code for each element in an array </li></ul></ul>
    5. 5. Control constructs -- while <ul><li>These are just like their counterparts in C </li></ul><ul><li>$i = 1; </li></ul><ul><li>while ( $i <= 10 ) </li></ul><ul><li>{ echo $i++; } </li></ul><ul><li>$i = 0; </li></ul><ul><li>do { print $i; } while ( $i>0 ) ; </li></ul>
    6. 6. Control constructs -- for <ul><li>These are just like their counterparts in C </li></ul><ul><li>for ($i = 1; $i <= 10; $i++) </li></ul><ul><li>{ print $i;} </li></ul>
    7. 7. Control constructs -- foreach <ul><li>These are similar their counterparts in Perl </li></ul><ul><li>foreach(array_expression as $value) </li></ul><ul><li>statement </li></ul><ul><li>foreach(array_expression as $key => $value) </li></ul><ul><li>statement </li></ul><ul><li><?php </li></ul><ul><li>$arr=array(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;); </li></ul><ul><li>foreach ($arr as $value) </li></ul><ul><li>{echo “Number: &quot; . $value . &quot;<br />&quot;;} </li></ul><ul><li>?> </li></ul>
    8. 8. Jumping in and out of PHP mode <ul><li>We can jump in and out of PHP mode even in the middle of a PHP block: </li></ul><ul><li><?php </li></ul><ul><li>if(strstr($HTTP_USER_AGENT,&quot;MSIE&quot;)) </li></ul><ul><li>{ ?> < p >You are using Internet Explorer</ p > < ?php } </li></ul><ul><li>else { ?> < p >You are not using Internet Explorer</ p > < ?php } </li></ul><ul><li>?> </li></ul><ul><li>Instead of using a n echo statement to print something, we jumped out of PHP mode . </li></ul><ul><li>N ote that the logical flow of the PHP remain s intact </li></ul><ul><ul><li>Only one of the HTML blocks will be sent to the user . </li></ul></ul>
    9. 9. A FORM and its handler in one <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Application Handler</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>if (! $_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?> </li></ul><ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li><p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> </li></ul><ul><li><p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> </li></ul><ul><li><input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </li></ul><ul><li></form> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>$sn = $_REQUEST['surname']; </li></ul><ul><li>echo &quot;<p>Thank you, $sn.</p>&quot;; </li></ul><ul><li>$addr = $_REQUEST['address']; </li></ul><ul><li>echo &quot;<p> We will write to you at $addr .</p>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
    10. 10. Finding out about your PHP environment <ul><li>One of the many pre-defined PHP functions is phpinfo() </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><h1>Your PHP Environment</h1> </li></ul><ul><li><?php phpinfo(); ?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li>In what follows, notice that mySQL support is enabled </li></ul>
    11. 11. Adding Comments to a PHP Script <ul><li>Comments are nonprinting lines placed in code such as: </li></ul><ul><ul><li>The name of the script </li></ul></ul><ul><ul><li>Your name and the date you created the program </li></ul></ul><ul><ul><li>Notes to yourself </li></ul></ul><ul><ul><li>Instructions to future programmers who might need to modify your work </li></ul></ul>
    12. 12. Adding Comments to a PHP Script (continued) <ul><li>Line comments hide a single line of code </li></ul><ul><ul><li>Add // or # before the text </li></ul></ul><ul><ul><li>Choose and stick with version </li></ul></ul><ul><li>Block comments hide multiple lines of code </li></ul><ul><ul><li>Add /* to the first line of code </li></ul></ul><ul><ul><li>And */ after the last character in the code </li></ul></ul>
    13. 13. Example Comments <ul><li><?php </li></ul><ul><li>/* </li></ul><ul><li>This line is part of the block comment. </li></ul><ul><li>This line is also part of the block comment. </li></ul><ul><li>*/ </li></ul><ul><li>echo (“<h1>Comments Example</h1>”); // Line comments can follow </li></ul><ul><li>code statements </li></ul><ul><li>// This line comment takes up an entire line. </li></ul><ul><li># This is another way of creating a line comment. </li></ul><ul><li>/* This is another way of creating </li></ul><ul><li>a block comment. */ </li></ul><ul><li>?> </li></ul>

    ×