Web app development_php_05

444 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
444
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
37
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Web app development_php_05

  1. 1. hasen@microcis.net July 07, 2013Hassen poreya Trainer, Cresco Solution Afghanistan Workforce Development Program PHP Operators, Expressions, Control Statement, Loops
  2. 2. Operators  A symbol that tells PHP to perform a mathematical or logical operation.  Arithmetic  Logical  Bitwise  Miscellaneous
  3. 3. Arithmetic Operators e.g, 27 / 3 15 % 4 23 % 6
  4. 4. Using a string with an Arithmetic Operator <?php $foo = "0"; // $foo is string $foo = $foo + 2; // $foo is now an integer: 2 $foo = $foo + 1.3; // $foo is now a float: 3.3 $foo = 5 + "10 Books"; // $foo is integer: 15 ?>
  5. 5. Using a string with an Arithmetic Operator ++ increment -- decrement  If an increment operator precedes the variable, the variable will be incremented prior to evaluation of the expression; otherwise, the variable isn’t operated on until after the value of the expression is computed. <?php $count=1; print($count++); //prints 1, but after print, count is 2 print(++$count); //prints 3 ?>
  6. 6. Logical and Relational Operators  Relational operators compare values and return either TRUE or FALSE.  Logical operators perform logical operations on TRUE and FALSE
  7. 7. The truth table for operators
  8. 8. Bitwise Operator  A bit is either 0 or 1  Bitwise operators view numbers from a binary perspective.  Operator Operation Performed & And | Or
  9. 9. Bitwise Operator <?php $x=14; $y=7; $z=$x & $y; echo “The bitwise (AND) of 14 and 7 is: ”.$z; ?>
  10. 10. Other Operators
  11. 11. Concatenation Operator  Connect two separated values together. <?php $result = “Passed”; $string = “The exam result of yours is:”.$result; print($string); ?>
  12. 12. Variable Variables with $ Operator <?php //set variables $var_name = "myValue"; $myValue = 123.456; print($$var_name . "<br />"); //prints "123.456“ ?>
  13. 13. Assignment Operators
  14. 14. Precedence of Operators <?php print((3+2*7) . “<br>”); //first multiplication print(((3+2)*7) . “<br>”); //first addition between parenthesis ?>
  15. 15. Expressions
  16. 16. Expressions  A bit of PHP that can be evaluated to produce a value.  PHP is an expression-oriented language  Everything is an expression  Expressions are combinations of identifiers and operators.
  17. 17. Expressions  Generally executed from left to right  Some operators are processed before others.  Use parentheses to force an operation to occur first  Two general rules  Some operators work only on certain data types.  If the operation is on a mix of an integer and a double, the integer will be converted to a double.
  18. 18. Expressions  PHP will even read doubles with an exponent. <?php print((1 + "1") . "<BR>"); print((1 + " 2") . "<BR>"); print((1 + "3extra stuff") . "<BR>"); print((1 + "4.5e6") . "<BR>"); print((1 + "a7") . "<BR>"); ?>
  19. 19. Evaluation  PHP will even read doubles with an exponent. <?php print((1 + "1") . "<BR>"); //1 + 1 == 2 print((1 + " 2") . "<BR>"); //1 + 2 == 3 print((1 + "3extra stuff") . "<BR>"); //1 + 3 == 4 print((1 + "4.5e6") . "<BR>"); //1 + 4500000 == 4500001 print((1 + "a7") . "<BR>"); //1 + 0 == 1 //“a7” begins with a letter, PHP treats it as zero! ?>
  20. 20. Control Statement
  21. 21. TRUE and FALSE  Zero (0 and 0.0) and an empty string ("") are considered to be FALSE.  Any other numerical value or string is TRUE.  Some control statements expect a Boolean value.
  22. 22. The if Statement If(condition/expression){ statements… }  You are not forced to put an elseif or an else statement after. <?php if(($day == “Friday") or ($day == “Saturday")) { print(“University is closed"); } ?>
  23. 23. If, ifelse, else Statements <?php if ($month == "January") { $num_month = 1; } elseif($month == "February") $num_month = 2; // March to October omitted here elseif($month == "November") $num_month = 11; else $num_month = 12; ?>
  24. 24. switch Statements Switch(expression){ case case-expression expression; break; case case-expression expression; break … default expression; }  The expression inside the switch statement is evaluated and then compared to each expression following a case expression.  A default statement works exactly like an else statement; it matches when no other case matches.  Break statement is used escape from the switch statement. Syntax Description
  25. 25. switch Statement <?php switch($month) { case "January": $num_month = 1; break; // February to October omitted here case "November": $num_month = 11; break; default: // It must be December $num_month = 12; } ?>
  26. 26. Loops  Loops allow you to repeat lines of code based on some condition.  Example  Read lines from a file until the end is reached.  Print some text a certain number of times.
  27. 27. while Loop Statement While (condition/expression){ statement; }  It is useful when you are not sure how many times you will need to iterate your statements.
  28. 28. while Loop Statement <?php $total = 0; $i = 1; while ($i <= 10){ $total += $i++; } echo "The sum of 1 to 10 is: " . $total; ?>
  29. 29. do while statement do{ statement; } while(condition/expression)  Similar to while loop.  First the statement is executed, and then acts according to the condition.
  30. 30. do while Statement <?php $total = 0; $i = 1; do { $total += $i++; } while ($i <= 10); echo "The sum of 1 to 10 is : " . $total; ?>
  31. 31. break Statement  When a break statement is encountered, the execution jumps to the outside of the loop or switch statement. <?php while (true){ echo “This line is printed here!”; break; echo “This line would never be printed”; } //These codes will be executed! ?>
  32. 32. break Statement <?php $i=0; $j=0; while ($i<10){ while($j<5){ if($j==5) break; // the loop is stopped $j++; // once $j reaches 5 } $i++; } echo „$i=‟.$i“<br>”; echo „$j=‟.$i; ?>
  33. 33. continue Statement  Similar to the break statement  But only the current execution of the loop is stopped, after that loop is continued. <?php while ($i++<5){ if($i==2) continue; echo $i; //2 will not be printed. } ?>
  34. 34. Let’s codesomething
  35. 35. Class exercise  Write a while loop that outputs the even numbers between 1 and 100.  Rewrite the script for Problem 1 using do while loop.  Write a while loop that outputs the numbers from 100 to 0 by 10's.
  36. 36. hasen@microcis.net July 07, 2013Hassen poreya Trainer, Cresco Solution Any Questions!

×