PHP Language Operators & Control Structures
Switch-case() statement <ul><li>Switch (decision-variable) </li></ul><ul><li>{ </li></ul><ul><li>case first condition is t...
<ul><li>Appropriate case() block execution </li></ul><ul><ul><li>Depend on the value of decision variable </li></ul></ul><...
Example <ul><li><?php // get form selection $day  =  $_GET [ 'day' ]; // check value and select appropriate item switch ( ...
While() loop <ul><li>While (condition is true) </li></ul><ul><li>{ </li></ul><ul><li>do this! </li></ul><ul><li>} </li></ul>
<ul><li>Execution of php statements within curly braces </li></ul><ul><ul><li>As long as the specified condition is true <...
Examples <ul><li><?php </li></ul><ul><li>$number = 5; </li></ul><ul><li>while   ($number  >= 2 ) </li></ul><ul><li>{ </li>...
Examples <ul><li>Variable is initialized to 5 </li></ul><ul><li>The while loop executes as long as the condition, ($number...
Output of example
<ul><li>The while() loop executes a set of statements while a specified condition is true. But what happens if the conditi...
Do-while() statement <ul><li>do {     do this! } while (condition is true) </li></ul>
While() vs do-while() <ul><li>Let's take a quick example to better understand the difference between while() and do-while(...
Examples <ul><li><?php $x  =  100 ; // while loop while ( $x  ==  700 ) {     echo  &quot;Running...&quot; ;     break; } ...
<ul><li>In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of ...
Example <ul><li><?php $x  =  100 ; // do-while loop do {     echo  &quot;Running...&quot; ;     break; } while ( $x  ==  7...
<ul><li>you would see  one  line of output, as the code within the do() block would run once.  </li></ul>
<ul><li><html> <head></head> <body> <?php // set variables from form input $upperLimit  =  $_POST [ 'limit' ]; $lowerLimit...
<ul><li>Thus, the construction of the do-while() loop is such that the statements within the loop are executed first, and ...
For() loops <ul><li>Both the while() and do-while() loops continue to iterate for as long as the specified conditional exp...
<ul><li>The for() loop typically looks like this:  </li></ul><ul><li>for (initial value of counter; condition; new value o...
Examples <ul><li><html> <head> <basefont face=&quot;Arial&quot;> </head> <body> <?php // define the number $number  =  13 ...
<ul><li>define the number to be used for the multiplication table.  </li></ul><ul><li>constructed a for() loop with $x as ...
Summary <ul><li>It is often desirable when writing code to perform different actions based on different decision. </li></u...
Summary <ul><li>In programming it is often necessary to repeat the same block of code a number of times. This can be accom...
Summary <ul><li>The  do...while  statement loops through a block of code as long as a specified condition is evaluated as ...
Summary <ul><li>The  for  statement loop is used when you know how many times you want to execute a statement or a list of...
<ul><li><html> <head></head> <body> <?php // get form selection $day  =  $_GET [ 'day' ]; // check value and select approp...
Upcoming SlideShare
Loading in …5
×

Php Operators N Controllers

1,825 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
1,825
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php Operators N Controllers

  1. 1. PHP Language Operators & Control Structures
  2. 2. Switch-case() statement <ul><li>Switch (decision-variable) </li></ul><ul><li>{ </li></ul><ul><li>case first condition is true: </li></ul><ul><li>do this! </li></ul><ul><li>case second condition is true: </li></ul><ul><li>do this! </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>. </li></ul><ul><li>… and so on … </li></ul><ul><li>} </li></ul>
  3. 3. <ul><li>Appropriate case() block execution </li></ul><ul><ul><li>Depend on the value of decision variable </li></ul></ul><ul><li>Default block </li></ul><ul><ul><li>Decision variable not match any of the listed case() conditions </li></ul></ul>
  4. 4. Example <ul><li><?php // get form selection $day = $_GET [ 'day' ]; // check value and select appropriate item switch ( $day ) {     case 1 :          $special = 'Chicken in oyster sauce' ;         break;     case 2 :          $special = 'French onion soup' ;         break;     case 3 :          $special = 'Pork chops with mashed potatoes and green salad' ;         break;     default:          $special = 'Fish and chips' ;         break; } ?> <h2>Today's special is:</h2> <?php echo $special ?> </body> </html> </li></ul>
  5. 5. While() loop <ul><li>While (condition is true) </li></ul><ul><li>{ </li></ul><ul><li>do this! </li></ul><ul><li>} </li></ul>
  6. 6. <ul><li>Execution of php statements within curly braces </li></ul><ul><ul><li>As long as the specified condition is true </li></ul></ul><ul><li>Condition becomes false </li></ul><ul><ul><li>The loop will be broken and the following statement will be executed. </li></ul></ul>
  7. 7. Examples <ul><li><?php </li></ul><ul><li>$number = 5; </li></ul><ul><li>while ($number >= 2 ) </li></ul><ul><li>{ </li></ul><ul><li>echo $number. &quot;<br/>&quot; ; </li></ul><ul><li>$number - = 1; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  8. 8. Examples <ul><li>Variable is initialized to 5 </li></ul><ul><li>The while loop executes as long as the condition, ($number>=2) is true </li></ul><ul><li>At the end of the loop block, the value of $number is decreased by 1. </li></ul>
  9. 9. Output of example
  10. 10. <ul><li>The while() loop executes a set of statements while a specified condition is true. But what happens if the condition is true on the first iteration of the loop itself? If you're in a situation where you need to execute a set of statements *at least* once, PHP offers you the do-while() loop. </li></ul>
  11. 11. Do-while() statement <ul><li>do {     do this! } while (condition is true) </li></ul>
  12. 12. While() vs do-while() <ul><li>Let's take a quick example to better understand the difference between while() and do-while(): </li></ul>
  13. 13. Examples <ul><li><?php $x = 100 ; // while loop while ( $x == 700 ) {     echo &quot;Running...&quot; ;     break; } ?> </li></ul>
  14. 14. <ul><li>In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of $x is not equal to 700. But, if you ran this version of the script: </li></ul>
  15. 15. Example <ul><li><?php $x = 100 ; // do-while loop do {     echo &quot;Running...&quot; ;     break; } while ( $x == 700 ); ?> </li></ul>
  16. 16. <ul><li>you would see one line of output, as the code within the do() block would run once. </li></ul>
  17. 17. <ul><li><html> <head></head> <body> <?php // set variables from form input $upperLimit = $_POST [ 'limit' ]; $lowerLimit = 1 ; // keep printing squares until lower limit = upper limit do {     echo ( $lowerLimit * $lowerLimit ). '&nbsp;' ;      $lowerLimit ++; } while ( $lowerLimit <= $upperLimit ); // print end marker echo ' END' ; ?> </body> </html> </li></ul>
  18. 18. <ul><li>Thus, the construction of the do-while() loop is such that the statements within the loop are executed first, and the condition to be tested is checked afterwards. This implies that the statements within the curly braces would be executed at least once. </li></ul>
  19. 19. For() loops <ul><li>Both the while() and do-while() loops continue to iterate for as long as the specified conditional expression remains true. But what if you need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <td> cells five times? In such cases, clever programmers reach for the for() loop... </li></ul>
  20. 20. <ul><li>The for() loop typically looks like this: </li></ul><ul><li>for (initial value of counter; condition; new value of counter) {     do this! } </li></ul>
  21. 21. Examples <ul><li><html> <head> <basefont face=&quot;Arial&quot;> </head> <body> <?php // define the number $number = 13 ; // use a for loop to calculate tables for that number for ( $x = 1 ; $x <= 10 ; $x ++) {     echo &quot;$number x $x = &quot; .( $number * $x ). &quot;<br />&quot; ; } ?> </body> </html> </li></ul>
  22. 22. <ul><li>define the number to be used for the multiplication table. </li></ul><ul><li>constructed a for() loop with $x as the counter variable, initialized it to 1. </li></ul><ul><li>specified that the loop should run no more than 10 times. </li></ul><ul><li>The auto-increment operator automatically increments the counter by 1 every time the loop is executed. Within the loop, the counter is multiplied by the number, </li></ul><ul><li>to create the multiplication table, and echo() is used to display the result on the page. </li></ul>
  23. 23. Summary <ul><li>It is often desirable when writing code to perform different actions based on different decision. </li></ul><ul><li>In addition to the if Statements, PHP includes a fourth type of conditional statement called the switch statement. The switch Statement is very similar or an alternative to the if...else if...else commands. The switch statement will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result. When testing a range of values, the if statement should be used. </li></ul>
  24. 24. Summary <ul><li>In programming it is often necessary to repeat the same block of code a number of times. This can be accomplished using looping statements. PHP includes several types of looping statements. </li></ul><ul><li>The while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true. </li></ul>
  25. 25. Summary <ul><li>The do...while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true. </li></ul><ul><li>The do...while loop is similar in nature to the While loop. The key difference is that the do...while loop body is guaranteed to run at least once. This is possible because the condition statement is evaluated at the end of the loop statement after the loop body is performed. </li></ul>
  26. 26. Summary <ul><li>The for statement loop is used when you know how many times you want to execute a statement or a list of statements. For this reason, the for loop is known as a definite loop. </li></ul>
  27. 27. <ul><li><html> <head></head> <body> <?php // get form selection $day = $_GET [ 'day' ]; // check value and select appropriate item switch ( $day ) {     case 1 :          $special = 'Chicken in oyster sauce' ;         break;     case 2 :          $special = 'French onion soup' ;         break;     case 3 :          $special = 'Pork chops with mashed potatoes and green salad' ;         break;     default:          $special = 'Fish and chips' ;         break; } ?> <h2>Today's special is:</h2> <?php echo $special ?> </body> </html> </li></ul>

×