Upload
anne-lee
View
703
Download
10
Embed Size (px)
Citation preview
Advanced PHP
PHP Operators Control Structures PHP Functions Arrays
*Property of STI K0032
OPERATOR is something that you feed with one or more values
characters or set of characters that perform a special operation within the PHP code
*Property of STI K0032
ARITHMETIC OPERATORSused to perform simple mathematical operations such as addition, subtraction, multiplication, division, etc
Example Name Result$a + $b Addition (+) Sum of $a and $b$a - $b Subtraction (-) Difference of $a
and $b$a * $b Multiplication
(*)Product of $a and $b
$a / $b Division (/) Quotient of $a and $b
$a % $b Modulus (%) Remainder of $a divided by $b
$a**$b Exponentiation (**)
Result of raising $a to the $b‘th power (PHP 5)
*Property of STI K0032
ASSIGNMENT OPERATORSused to transfer values to a variable
Operator
Assignment Same as Description
= a=b a = b
The left operand gets the value of the expression on the right
+= a+=b a = a + b Addition-= a-=b a = a – b Subtraction*= a*=b a = a * b Multiplication/= a/=b a = a / b Division%= a%=b a = a % b Modulus
*Property of STI K0032
COMPARISON OPERATORSallow you to compare two values
Example Name Result$a = = $b Equal TRUE if $a is equal to $b.$a = = = $b
Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)
$a != $b Not equal TRUE if $a is not equal to $b.$a <> $b Not equal TRUE if $a is not equal to $b.$a != = $b
Not identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
$a < $b Less than TRUE if $a is strictly less than $b.$a > $b Greater than TRUE if $a is strictly greater than
$b.
$a <= $b Less than or equal
TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal
TRUE if $a is greater than or equal to $b.
*Property of STI K0032
INCREMENT/DECREMENT OPERATORS
increment operators are used to increase the value of a variable by 1
decrement operators are used to decrease the value of a variable by 1
Example
Name Effect
++$a Pre-increment Increment $a by one. Then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
*Property of STI K0032
INCREMENT/DECREMENT OPERATORS
Sample script<?phpecho "<h3>Postincrement</h3>";$a = 5;echo "Should be 5: " . $a++ . "<br />\n";echo "Should be 6: " . $a . "<br />\n";
echo "<h3>Preincrement</h3>";$a = 5;echo "Should be 6: " . ++$a . "<br />\n";echo "Should be 6: " . $a . "<br />\n";?
*Property of STI K0032
LOGICAL OPERATORSused to combine conditional statements
Example Name Result$a and $b AND TRUE if both $a and $b are
TRUE.$a or $b OR TRUE if either $a or $b is TRUE.$a xor $b XOR TRUE if either $a or $b is TRUE,
but not both.! $a NOT TRUE if $a is not TRUE.$a && $b AND TRUE if both $a and $b are
TRUE.$a | | $b OR TRUE if either $a or $b is TRUE.
*Property of STI K0032
STRING OPERATORS two string operators
concatenation operator (.) - returns the concatenation of its right and left arguments
concatenation assignment operator (.=) - appends the argument on the right side to the argument on the left side<?php$a = "Hello ";$b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello ";$a .= "World!"; // now $a contains "Hello World!"?>
*Property of STI K0032
PHP ARRAY OPERATORSPHP array operators are used to compare arraysExample Name Result
$a + $b Union (+) Union of $a and $b$a = = $b Equality (==) Returns true of $a and $b have
the same value$a === $b Identity (===) Returns true if $a and $b have
the same value, same order, and of the same type
$a != $b Inequality (!=) Returns true if $a is not equal to $b
$a <> $b Inequality (<>) Returns true if $a is not equal to $b
$a !== $b Not-Identity (!==)
Returns true if $a is not identical to $b
*Property of STI K0032
CONTROL STRUCTURES: IF STATEMENT
The if statement is one of the most important features of many languages
It allows conditional execution of code fragments
Use PHP if statement when you want your program to execute a block of code only if a specified condition is true
*Property of STI K0032
IF STATEMENTSyntax:
Example:
If (condition) {statement to be executed if condition is true;}
<?php$t = date("H");if ($t < "12") { echo "Good morning!";}?>
*Property of STI K0032
IF…ELSE STATEMENT This control structure execute some code if a condition is met and do something else if the condition is not met
Else extends an if statement to execute a statement in case the condition in the if statement evaluates to false
Syntax:If (condition) {statement to be executed if condition
is true;} else {
Statement to be executed if condition is false;}
*Property of STI K0032
IF…ELSE STATEMENTExample:
The else statement is only executed if the condition in the if statement is evaluated to false, and if there were any elseif expressions – only if they evaluated to false as well
<?php$t = date("H");if ($t < "12") { echo "Good morning!";} else {
echo “Good afternoon!”;}?>
*Property of STI K0032
ELSEIF STATEMENTa combination of if and else it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE
Syntax:If (condition) {
statement to be executed if condition is true;} elseif (condition) {
Statement to be executed if condition is true;} else {
Statement to be executed if condition is false;}
*Property of STI K0032
ELSEIF STATEMENTExample:<?php$t = date("H");
if ($t < "12") { echo "Good morning!";} elseif ($t < "18") { echo "Good afternoon!";} else { echo "Have a good evening!";}?>
*Property of STI K0032
SWITCH STATEMENT similar to a series of if statements on the same expression
Syntax:switch (n) {case 1:
Statement to be executed if n=case 1;Break;
case 2:Statement to be executed if n=case 2;Break;
case 3:Statement to be executed if n=case 3;Break;
...Default:Statement to be executed if n is not equal to all
cases;}
*Property of STI K0032
SWITCH STATEMENTExample:
<?php$fruit = "mango";switch ($fruit) { case "apple":
echo "My favorite fruit is apple!"; break; case "banana":
echo " My favorite fruit is banana!"; break; case "mango":
echo " My favorite fruit is mango!"; break; default:
echo "My favorite fruit is not in the list!";}?>
*Property of STI K0032
WHILE STATEMENTWhile loops are the simplest type of loop in PHP. They behave like their C counterparts
The basic form of a while statement isWhile (condition is true) {
Statement to be executed here...;}
*Property of STI K0032
WHILE STATEMENTExample:
<?php$a = 1;While ($a <=10) {
echo “The number is: $a <br>”;$a++;
}?>
*Property of STI K0032
DO…WHILE STATEMENTdo...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
do…while statement will always execute the blocked of code once, it will then checked the condition, and repeat the loop while the condition is true
*Property of STI K0032
DO…WHILE STATEMENTSyntax:
Example:
Do {Statement to be executed;
} while (condition is true);
<?php$a = 1;Do {
echo “The number is: $a <br>”;$a++; }While ($a <=10);?>
*Property of STI K0032
FOR STATEMENTexecutes block of codes in a specified number of times
basically used when you know the number of times the code loop should run
Syntax:for (initialization; condition; increment) {
statement to be executed}
*Property of STI K0032
FOR STATEMENT
Initialization: It is mostly used to set counter
Condition: It is evaluated for each loop iteration
Increment: Mostly used to increment a counter
*Property of STI K0032
FOR STATEMENTExample:
Result:
<?php for ($a = 1; $a <= 5; $a++) { echo "The number is: $a <br>";} ?>
*Property of STI K0032
FUNCTIONself-contained blocked of codes that perform a specified “function” or task
executed by a call to the functioncan be called anywhere within a pageoften accepts one or more parameters (“also referred to as arguments”) which you can pass to it
*Property of STI K0032
FUNCTIONSyntax:
The declaration of a user defined function starts with the word “function” followed by a short but descriptive name for that function
function functionName(){
Statement to be executed;}
*Property of STI K0032
FUNCTIONExample:
Result:
<?phpfunction callMyName() { echo "Francesca Custodio"; }callMyName();?>
*Property of STI K0032
FUNCTION ARGUMENTSFunction arguments are just like variables specified right after the function name inside the parentheses
Example:<?phpfunction MyFamName($Fname){echo "$Fname Garcia. <br>”; }MyFamName("Allen");MyFamName("Patrick");MyFamName("Hannah");?>
*Property of STI K0032
FUNCTION RETURNS A VALUEFunction returns a value using the return statement
Example:<?phpfunction sum($a, $b) { $ab = $a + $b; return $ab;}echo "5 + 10 = " . sum(5, 10) . "<br>";echo "7 + 13 = " . sum(7, 13) . "<br>";echo "2 + 4 = " . sum(2, 4);?>
*Property of STI K0032
Having 3 car names (Honda, Mazda, and Mitsubishi), how will you store these car names in a variable?
Answer:$car1 = “Honda”;$car2 = “Mazda”;$car3 = “Mitsubishi”;
However, what if you want to loop through the cars and look for a specific one? And what if you had hundreds or thousands of cars? What will you do?
Answer:Create an array and store them to a single
variable.
*Property of STI K0032
ARRAYSPHP array is a special variable which allows you to store multiple values in a single variable$cars = array(“Honda”, “Mazda”, “Mitsubishi”);
*Property of STI K0032
ARRAYSThe values of an array can be accessed by referring to its index number
Result:
<?php$cars = array("Honda", "Mazda", "Mitsubishi"); echo "I have " . $cars[0] . ", " . $cars[1] “,” . ", and " . $cars[2] . ".";?>
*Property of STI K0032
TYPES OF PHP ARRAYSThree (3) different types of PHP arrays
Indexed arrayAssociative arrayMultidimensional array
*Property of STI K0032
INDEXED ARRAYSIndexed arrays or numeric arrays use number as key
The key is the unique identifier, or id of each item within an array
index number starts at zero
*Property of STI K0032
INDEXED ARRAYSTwo ways to create an array:
1. Automatic key assignment
2. Manual key assignment
$Fruits = array(“Mango”, ”Santol”, ”Apple”, “Banana”);
$fruit[0] = “Mango”;$fruit[1] = “Santol”;$fruit[2] = “Apple”;$fruit[3] = “Banana”;
*Property of STI K0032
ARRAY COUNT() FUNCTIONThe count() function is used to return the length (the number of elements) of an arrayExample:
Result:
$fruit = array(“Mango”, ”Santol”, ”Apple”, “Banana”);$arrlength=count($fruit);echo $arrlength;
*Property of STI K0032
ARRAY: DISPLAYING SPECIFIC CONTENT
Example:
Result:
$Fruit = array(“Mango”, ”Santol”, ”Apple”, “Banana”);echo $fruit[1];
*Property of STI K0032
LOOPING THROUGH INDEXED ARRAY
Loop construct is used to loop through and print all the values of a numeric array
For loop structure is best to use in numeric array<?php$fruit = array("Mango", "Santol", "Apple", "Banana");
$arrlength = count($fruit);for ($i = 0; $i < $arrlength; $i++){
echo $fruit[$i];echo "<br>";
}?>
*Property of STI K0032
ASSOCIATIVE ARRAYSarrays that use named keys as you assigned to them
Associative arrays are similar to numeric arrays but instead of using a number for the key
it use a value. Then assign another value to the key
*Property of STI K0032
ASSOCIATIVE ARRAYSTwo options to create an associative array1. First:
2. Second:
$fruit = array (“Mango”=>”100”, “Santol”=>”200”, “Apple”=>”300”);
$fruit [‘Mango’] = “100”;$fruit [‘Santol’] = “200”;$fruit [‘Apple’] = “300”;
*Property of STI K0032
DISPLAYING THE CONTENT OF AN ASSOCIATIVE ARRAY
Displaying the content of an associative array is compared with numeric or indexed array, by referring to its key
Example:
Output:
<?php$fruit = array("Mango" => "100 per kilo", "Santol" => "200 per kilo", "Apple" => "300 per kilo");
echo "Santol:" . $fruit["Santol"];?>
*Property of STI K0032
LOOPING THROUGH AN ASSOCIATIVE ARRAY
Loop construct is used to loop through and print all the values of an associative array
For each loop structure is best to use in associative array
Example:<?php$fruit = array("Mango", "Santol", "Apple", "Banana");$arrlength = count($fruit);foreach($fruit as $x => $x_value) {
echo "Key = " . $x . ", Value = " . $x_value;echo "<br>";
}?>
*Property of STI K0032
MULTIDIMENSIONAL ARRAYSan array that contains another array as a value, which in turn can hold another array as well
This can be done as many times as can be wish – could have an array inside another array, which is inside another array, etc
In such a way, it is possible to create two- or three-dimensional arrays
*Property of STI K0032
MULTIDIMENSIONAL ARRAYSName QTY Sold
Mango 100 96Apple 60 59Santol 110 100<?php$fruit = array ( array("Mango",100,96), array("Apple",60,59), array("Santol",110,100) );?>
*Property of STI K0032
MULTIDIMENSIONAL ARRAYS The two-dimensional array name $fruit array which has two indices: row and column - contains three arrays. To get access to each specific element of the $fruit array, one must point to the two indices.
Example:
Result:
echo $fruit[0][0].": QTY: ".$fruit[0][1].", sold: ".$fruit[0][2].".<br>";echo $fruit[1][0].": QTY: ".$fruit[1][1].", sold: ".$fruit[1][2].".<br>";echo $fruit[2][0].": QTY: ".$fruit[2][1].", sold: ".$fruit[2][2].".<br>";
*Property of STI K0032
MULTIDIMENSIONAL ARRAYS Using for loop statement to get the element of the $fruit array
Result:
for ($row = 0; $row < 3; $row++) { echo "<p><b>Row number $row</b></p>"; echo "<ul>"; for ($col = 0; $col < 3; $col++) { echo "<li>".$fruit[$row][$col]."</li>"; } echo "</ul>";}