1 / 28

IS6116 - PHP

IS6116 - PHP. Variables. Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. .

falala
Download Presentation

IS6116 - PHP

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. IS6116 - PHP

  2. Variables • Variables in PHP are represented by a dollar sign followed by the name of the variable. • The variable name is case-sensitive. • A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

  3. Declaring a Variable Using PHP <?php $var = "Bob"; $Var = "Joe"; echo "$var, $Var"; // outputs "Bob, Joe" $4site = 'not yet'; // invalid; starts with a number $_4site = 'not yet'; // valid; starts with an underscore ?>

  4. Variable Types • A variable type refers to the kind of data that is stored in it. • PHP data types include: • Integer • Double • String • Array • Object

  5. Type Strength • In PHP, the type of the variable is determined by the value assigned to it. • E.g. $qty1=0; // variable called qty1 holds an integer value $amount1=0.00; //variable called amount1 holds a double value But if we redeclare amount1 $amount1=“Hello”; //The variable amount1 would now be a string

  6. Types Casting • You can pretend that a variable or value is temporarily a different type by using a type cast. $qty1=0; $amount1=double(qty1); • This means that the value stored in qty1 is interpreted as a double and stored as with the variable name amount1.

  7. Variable Scope • Scope refers to a place in the script where the variable is visible. • Global variables declared in a script are visible throughout the script but not inside functions. • Variables used inside functions are local to functions. • Variables used inside functions that are decalred as global refer to the global variable of the same name.

  8. Constants • Once the value of a constant is set it may not be changed. • What might you store as a constant in a script? • Use the define function to store constant values: Define((“cdprice”), 15)

  9. Arithmetic Operators • They are usually applied to integers or doubles if you apply an arithmetic operator to a string PHP will try and convert it to a number.

  10. Arithmetic Operators

  11. String Concatenation/Operator • Used to add two strings together. • Syntax: <? echo $qty1.“Purchased<br>”; ?> • To avoid having to write multiple echo commands use the . (i.e. a fullstop)

  12. Example 2 • $a=“PHP”; • $b=“is great”; • $result=$a.$b; Output: PHPisgreat

  13. Comparison Operators

  14. Logical Operators

  15. Control Structures • Are structures within a programming language that allow us to control the flow of execution through a program or script. • The constructs that tell the programs to make decisions are known as conditional statements.

  16. If Statements • If statements may be used to make a decision. • If the condition is true the following code block is executed. <?php if ($a > $b) echo "a is bigger than b"; ?>

  17. Else Statements • This allows to provide an alternative action when the condition of the if statement is false. <?php if ($a > $b) { echo "a is bigger than b"; } else { echo "a is NOT bigger than b"; } ?>

  18. elseif Statements <?php if ($a > $b) { echo"a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } ?>

  19. Switch statements • The switch statement is similar to a series of IF statements on the same expression. • You may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to.

  20. <?php switch ($a) { case 0: echo "a equals 0"; break; case 1: echo "a equals 1"; break; case 2: echo "a equals 2"; break; default: echo "a is not equal to 0, 1 or 2"; } ?>

  21. Iteration:While loops • It tells PHP to execute the nested statement(s) repeatedly, while the expression evaluates to true. • The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration. • If the while expression evaluates to false from the very beginning, the nested statement(s) won't even be run once.

  22. While Loops <?php $i = 1; while ($i <= 10) { echo $i; $i++; //Same as writing $i = $i + 1; } ?>

  23. For Loops • for (expr1; expr2; expr3) statement • The first expression (expr1) is evaluated once unconditionally at the beginning of the loop. • In the beginning of each iteration, expr2 is evaluated. If it evaluates to true, the loop continues and the nested statement(s) are executed. If it evaluates to false, the execution of the loop ends. • At the end of each iteration, expr3 is evaluated (executed).

  24. For Loops <?php for ($i = 1; $i <= 10; $i++) { echo $i; } ?>

  25. Do…While Loops • do..while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. • The main difference from regular while loops is that the first iteration of a do..while loop is guaranteed to run.

  26. Do..While Loops <?php $i = 0; do { echo $i; } while ($i > 0); ?>

  27. ? Operator • Similar to the if statement but returns a value derived from one of two expressions by a colon. • Syntax: (expression) ? Returned_if_expression_is_true: returned_if_expression_is_false;

  28. <html> <head> <title>Example</title> <body> <?php $mood=“sad”; $text=($mood==“happy”)?”I’m in a good mood”:”Not happy but $mood”; print “$text”; ?> </body> </html> Example

More Related