Download presentation
Presentation is loading. Please wait.
1
String Conversion and Type Juggling
If you perform s numerical operation on a string, PHP will evaluate the string as a number. This is known as string conversion ,although the variable containing the String itself my not necessarily change. Lesson 2
2
String Conversion and Type Juggling
Only the beginning of the string is evaluated as a number. If the string begins with A valid numerical value, the string will evaluate as that value; otherwise it will Evaluate as zero. The string “33 students” would evaluate as 3 if used in a numerical Operation, but the string “students 33” would evaluate 0 (zero). Lesson 2
3
String Conversion and Type Juggling
PHP performs type juggling between the two numeric types. If you perform a numerical operation between integer and double the result will be a double. $a=1; // $a is an integer $b= //$b is a double; $c=$a+$b; //$c is a double (value 3.0); $d=$c+”33 students” //$d is a double (value 36) Lesson 2
4
Type Casting Type casting allows you to explicitly change the data type of a variable. $a=12.3; // $a is a double $a=(int) $a; //$a is an integer (value 12); $a=(double) $a; //$a is a double (value 12.0); $b=(string) $a //$b is a string (value “12”) Lesson 2
5
Useful Functions for variables
PHP has a number of build-in functions for working with variables. gettype() Determines the data type of a variable. It returns one of the following values. integer double string array object class unknown type Lesson 2
6
Useful Functions for variables
Lesson 2
7
Useful Functions for variables
settype() The settype() function explicitly sets type of a variable Lesson 2
8
Useful Functions for variables
isset() and unset() Unset() is used to destroy a variable, freeing all the memory that is associated with the variable so it is then available again, The isset() function is used to determine whether A variable has been given a value. If a value has been set then it returns true. Lesson 2
9
Useful Functions for variables
Lesson 2
10
Useful Functions for variables
Lesson 2
11
Useful Functions for variables
empty() Empty() is nearly the opposite of isset(). It returns true if the variable is not set Or has a value of zero or an empty string. Other wise it returns false. Lesson 2
12
Useful Functions for variables
Operators Lesson 2
13
Arithmetic Operators Like every programming language, PHP uses the basic mathematical operators Operator Operation Performed Example Addition Subtraction * Multiplication *3 / Division /3 % Modulus %3 Lesson 2
14
Arithmetic Operators The Unary Operator
The minus sign(-) is also used with a single numeric value to negate a number ( The is to make a positive number negative, or a negative number positive) $a = 3; $b= - $a; //$b=-3 $c= -5; $d= -$c; //$d=5 Lesson 2
15
Arithmetic Operators $i=5; $i=5; if($i==8) if($i=8) echo “Eight”;
else echo “five”;// this prints five $i=5; if($i=8) echo “Eight”; //”eight” prints every time Lesson 2
16
Logical Operator Example Operator Name Evaluate true when:
$a && $b And Both $a and $b evaluate to true $a || $b Or one or both of $a and $b evaluate to true $a and $b And Both $a and $b evaluate to true $a or $b Or one or both of $a and $b evaluate to true $a xor $b Excusive Or one of $a and $b evaluates to true, but not both ! $a Not $a does not evaluate to true Lesson 2
17
Logical Operator Lesson 2
18
String Concatenation Operator
Lesson 2
19
Variable Assignment Shortcuts
Lesson 2
20
Variable Assignment Shortcuts
Lesson 2
21
Variable Assignment Shortcuts
Lesson 2
22
If Statements $country=“us”; if ($country==“us”) echo “USA”;
If more one statement is to be executed when the condition is true, curly braces {} Are used to indicate which lines belong inside the if block: $country=“us”; if ($country==“us”) { echo “USA”; echo “Canada”; } Lesson 2
23
If Statements Any Condition which is not met, zero, an empty string (“”), undefined values, and the Built in constant false all evaluate to false If (3<2) Echo “this will not print”; If (false) If(“0”) if($x) If (3>2) Echo “this will print”; If (“false”) If(“00”) if(0==0) Lesson 2
24
If Statements Branches Conditions
If the tested condition returns false, PHP allows us to specify another block of code to be executed using the else keyword. If($x<0) { echo “Negative” } else echo “Positive”; Lesson 2
25
If Statements PHP also provides the elseif keyword to test alternative conditions if the condition In the if portion of the statement is not true. Any number of elseif statements may be Used with an if statement. The final else branch then allows us to specify code that Should be executed if none of the if or elseif condition is true If ($x < 0) { echo “Negative”; } elseif ($x==0) echo “Zero”; else echo “Positive” Lesson 2
26
If Statements It is also common to nest if statements within another if statement $studentName=“Adam”; $pass=‘123; If ($studentName==“Adam”) { if($pass==123) echo “login successful”; } else echo “Retype your password”; echo “login failed”; Lesson 2
27
If Statements $studentName=“Adam”; $pass=‘123;
If ($studentName==“Adam” && $pass==123) { echo “login successful”; } else echo “login failed”; Lesson 2
28
switch Statements Switch Statement $val=6; $x=5; $y=6; switch ($val) {
case $x: echo “five”; break; case $y: echo “six”; default: echo $val; } Lesson 2
29
Loops While Loops While (condition) { //statements } $x=4;
While ($x != 0) { echo $x; echo “<br>”; --$x; } echo “done”; Lesson 2
30
Loops do..While Loops $x=4; do { echo $x; echo “<br>”; --$x;
} while ($x != 0); echo “done”; Lesson 2
31
Loops for Loops $x=4; for ($i=0;$i<=$x;$i++) {
echo $i."<br>"; } Lesson 2
32
Loops for Loops $x=4; for ($i=0;$i<=$x;$i++) {
echo $i."<br>"; } Lesson 2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.