1 / 26

Control Structures

Control Structures. If If else Else if While Do while. For loop. // Example One for ($ kilometers = 1; $ kilometers <= 5; $ kilometers ++) { printf ("%d kilometers = %f miles < br />", $ kilometers , $ kilometers *0.62140); }. // Example Two

hargis
Download Presentation

Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures

  2. If • If else • Else if • While • Do while

  3. For loop • // Example One • for ($kilometers = 1; $kilometers <= 5; $kilometers++) { • printf("%d kilometers = %f miles <br />", $kilometers, $kilometers*0.62140); • }

  4. // Example Two • for ($kilometers = 1; ; $kilometers++) { • if ($kilometers > 5) break; • printf("%d kilometers = %f miles <br />", $kilometers, $kilometers*0.62140); • }

  5. // Example Three • $kilometers = 1; • for (;;) { • // if $kilometers > 5 break out of the for loop. • if ($kilometers > 5) break; • printf("%d kilometers = %f miles <br />", $kilometers, $kilometers*0.62140); • $kilometers++; • }

  6. The foreach Statement • The foreach looping construct syntax is adept at looping through arrays, pulling each key/value pair from the array until all items have been retrieved or some other internal conditional has been met. • The following is its syntax: • foreach (array_expr as $value) { • statement • }

  7. Example • <?php$colors = array("red","green","blue","yellow"); foreach ($colors as $value)   {   echo "$value <br>";   }?> 

  8. The second variation is well-suited for working with both the key and value of an array. The syntax follows: • foreach (array_expr as $key => $value) { • statement • }

  9. Example • <?php • $links = array("The Apache Web Server" => "www.apache.org", • "Apress" => "www.apress.com", • "The PHP Scripting Language" => "www.php.net"); • echo "<b>Online Resources</b>:<br />"; • foreach($links as $title => $link) { • echo "<a href=\"http://$link\">$title</a><br />"; • } • ?>

  10. The switch Statement • <?php • $category="newss"; • switch($category) { • case "news": • echo "<p>What's happening around the world</p>"; • break; • case "weather": • echo "<p>Your weekly forecast</p>"; • break; • case "sports": • echo "<p>Latest sports highlights</p>"; • break; • default: • echo "<p>Welcome to my Web site</p>"; • } • ?>

  11. Note the presence of the break statement at the conclusion of each case block. If a break statement isn’t present, all subsequent case blocks will execute until a break statement is located. • <?php • $category=" weather "; • switch($category) { • case "news": • echo "<p>What's happening around the world</p>"; • case "weather": • echo "<p>Your weekly forecast</p>"; • case "sports": • echo "<p>Latest sports highlights</p>"; • default: • echo "<p>Welcome to my Web site</p>"; • } • ?>

  12. The break and goto Statements • <?phpgoto a;echo 'Foo';a:echo 'Bar';?>

  13. The continue Statement • The continue statement works similarly to break, with one essential difference: it ends only the current iteration. After a continue statement, the loop starts over at the condition evaluation. • <?php • $usernames = array("grace","doris","gary","nate","missing","tom"); • for ($x=0; $x < count($usernames); $x++) { • if ($usernames[$x] == "missing") continue;//break • printf("Staff member: %s <br />", $usernames[$x]); • } • ?>

  14. Another Ex • <?php • $x = 0; • while ($x < 10) { • ++$x; • if ($x % 2 == 0) { • continue; • echo "$x is an even number.\n"; • } • echo "$x is an odd number.<br>"; • } • ?>

  15. Ternary Operator in PHP ? • Consisting of three parts, the ternary operator uses three expressions separated by a question mark and a colon. The question mark follows the test expression and can be thought of as asking, “Well, is it true?” The colon then separates your two possible values, the first of which will be chosen if the test expression is true, and the second if the test expression is false. • Syntax • $variable = ( condition ) ? true : false;

  16. Ex • <?php • if ($coolFactor >= 10) { • $message = "You are one cool dude!"; • } • else { • $message = "Sorry, you aren't that cool!"; • } • ?> • Equivalent in ternary: • <?php • $coolFactor=90; • $message = ($coolFactor >= 10) ? "You're one cool dude!" : "Sorry, you aren't that"; • echo $message; • ?>

  17. Variable Scope • Local variables • • Function parameters • • Global variables • • Static variables

  18. Local Variables • A variable declared in a function is considered local. That is, it can be referenced only in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function. Note that when you exit the function in which a local variable has been declared, that variable and its corresponding value are destroyed.

  19. Example • <?php • $x = 4; • function assignx () { • $x = 0; • printf("\$x inside function is %d <br />", $x);//0 • } • assignx(); • printf("\$x outside of function is %d <br />", $x);//4 • ?>

  20. Global Variables • In contrast to local variables, a global variable can be accessed in any part of the program. To modify a global variable, however, it must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword GLOBAL in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name.

  21. Example • <?php • $somevar = 15; • function addit() { • GLOBAL $somevar; • $somevar++; • echo "Somevar is $somevar"; • } • addit(); • ?> • //16

  22. Static Variables • The final type of variable scoping to discuss is known as static. In contrast to the variables declared as function parameters, which are destroyed on the function’s exit, a static variable does not lose its value when the function exits and will still hold that value if the function is called again. You can declare a variable as static simply by placing the keyword STATIC in front of the variable name:

  23. Example • <?php • function keep_track() { • STATIC $count = 0; • $count++; • echo $count; • echo "<br />"; • } • keep_track();//1 • keep_track();//2 • keep_track();//3 • ?>

  24. Constants • A constant is a value that cannot be modified throughout the execution of a program. Constants are particularly useful when working with values that definitely will not require modification, such as pi (3.141592) • Defining a Constant • The define() function defines a constant by assigning a value to a name. • <?php • define("PI", 3.141592); • //The constant is subsequently used in the following listing: • printf("The value of pi is %f<br>", PI); • $pi2 = 2 * PI; • printf("Pi doubled equals %f", $pi2); • ?>

  25. Operands • Operands are the inputs of an expression. You might already be familiar with the manipulation and use of operands not only through everyday mathematical calculations, but also through prior programming experience. Some examples of operands follow: • $a++; // $a is the operand • $sum = $val1 + val2; // $sum, $val1 and $val2 are operands

  26. Operators • An operator is a symbol that specifies a particular action in an expression.

More Related