Control Structures In Php 2

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Control Structures In Php 2 - Presentation Transcript

    1. Control Structures in PHP
    2. 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“; }
    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 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 .
    9. 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>
    10. 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
    11. 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
    12. 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
    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. */
      • ?>
    SlideShare Zeitgeist 2009

    + Keith  FeigheryKeith Feighery Nominate

    custom

    451 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 451
      • 451 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 4
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories