280 likes | 412 Views
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. .
E N D
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.
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 ?>
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
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
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.
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.
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)
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.
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)
Example 2 • $a=“PHP”; • $b=“is great”; • $result=$a.$b; Output: PHPisgreat
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.
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"; ?>
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"; } ?>
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"; } ?>
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.
<?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"; } ?>
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.
While Loops <?php $i = 1; while ($i <= 10) { echo $i; $i++; //Same as writing $i = $i + 1; } ?>
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).
For Loops <?php for ($i = 1; $i <= 10; $i++) { echo $i; } ?>
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.
Do..While Loops <?php $i = 0; do { echo $i; } while ($i > 0); ?>
? 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;
<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