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 Operators
used 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 Operators
used 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 Operators
allow 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
<?php
echo "<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 Operators
used to combine conditional statements
Example Name Result
$a and $b AND TRUE if both $a and $b areTRUE.
$a or $b OR TRUE if either $a or $b isTRUE.
$a xor $b XOR TRUE if either $a or $b isTRUE, but not
both.
! $a NOT TRUE if $a is notTRUE.
$a && $b AND TRUE if both $a and $b areTRUE.
$a | | $b OR TRUE if either $a or $b isTRUE.
*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 Operators
PHP array operators are used to compare arrays
Example 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 Statement
Syntax:
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 Statement
Example:
 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 Statement
 a 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 Statement
 Example:
<?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 Statement
 Example:
<?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 Statement
While loops are the simplest type of loop in PHP.
They behave like their C counterparts
The basic form of a while statement is
While (condition is true) {
Statement to be executed here...;
}
*Property of STI K0032
While Statement
Example:
<?php
$a = 1;
While ($a <=10) {
echo “The number is: $a <br>”;
$a++;
}
?>
*Property of STI K0032
Do…while statement
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
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 statement
Syntax:
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 statement
executes 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 statement
Example:
Result:
<?php
for ($a = 1; $a <= 5; $a++) {
echo "The number is: $a <br>";
}
?>
*Property of STI K0032
Function
self-contained blocked of codes that
perform a specified “function” or task
executed by a call to the function
can be called anywhere within a page
often accepts one or more parameters
(“also referred to as arguments”) which
you can pass to it
*Property of STI K0032
Function
 Syntax:
 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
Function
 Example:
 Result:
<?php
function callMyName()
{
echo "Francesca Custodio";
}
callMyName();
?>
*Property of STI K0032
Function Arguments
 Function arguments are just like variables
 specified right after the function name inside the
parentheses
 Example:
<?php
function MyFamName($Fname){
echo "$Fname Garcia. <br>”;
}
MyFamName("Allen");
MyFamName("Patrick");
MyFamName("Hannah");
?>
*Property of STI K0032
Function returns a value
Function returns a value using the return
statement
Example:
<?php
function 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
Arrays
PHP 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
Arrays
The 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 Arrays
Three (3) different types of PHP arrays
Indexed array
Associative array
Multidimensional array
*Property of STI K0032
Indexed Arrays
Indexed 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 Arrays
Two 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() function
The count() function is used to return the length (the
number of elements) of an array
Example:
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 Arrays
arrays 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 Arrays
Two options to create an associative array
1. 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 Arrays
an 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 Arrays
Name QTY Sold
Mango 100 96
Apple 60 59
Santol 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>";
}

Advanced php

  • 1.
    Advanced PHP  PHPOperators  Control Structures  PHP Functions  Arrays
  • 2.
    *Property of STIK0032 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
  • 3.
    *Property of STIK0032 Arithmetic Operators used 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)
  • 4.
    *Property of STIK0032 Assignment Operators used 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
  • 5.
    *Property of STIK0032 Comparison Operators allow 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.
  • 6.
    *Property of STIK0032 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.
  • 7.
    *Property of STIK0032 Increment/Decrement Operators Sample script <?php echo "<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"; ?
  • 8.
    *Property of STIK0032 Logical Operators used to combine conditional statements Example Name Result $a and $b AND TRUE if both $a and $b areTRUE. $a or $b OR TRUE if either $a or $b isTRUE. $a xor $b XOR TRUE if either $a or $b isTRUE, but not both. ! $a NOT TRUE if $a is notTRUE. $a && $b AND TRUE if both $a and $b areTRUE. $a | | $b OR TRUE if either $a or $b isTRUE.
  • 9.
    *Property of STIK0032 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!" ?>
  • 10.
    *Property of STIK0032 PHP Array Operators PHP array operators are used to compare arrays Example 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
  • 11.
    *Property of STIK0032 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
  • 12.
    *Property of STIK0032 IF Statement Syntax: Example: If (condition) { statement to be executed if condition is true; } <?php $t = date("H"); if ($t < "12") { echo "Good morning!"; } ?>
  • 13.
    *Property of STIK0032 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; }
  • 14.
    *Property of STIK0032 If…Else Statement Example:  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!”; } ?>
  • 15.
    *Property of STIK0032 ElseIf Statement  a 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; }
  • 16.
    *Property of STIK0032 ElseIf Statement  Example: <?php $t = date("H"); if ($t < "12") { echo "Good morning!"; } elseif ($t < "18") { echo "Good afternoon!"; } else { echo "Have a good evening!"; } ?>
  • 17.
    *Property of STIK0032 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; }
  • 18.
    *Property of STIK0032 Switch Statement  Example: <?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!"; } ?>
  • 19.
    *Property of STIK0032 While Statement While loops are the simplest type of loop in PHP. They behave like their C counterparts The basic form of a while statement is While (condition is true) { Statement to be executed here...; }
  • 20.
    *Property of STIK0032 While Statement Example: <?php $a = 1; While ($a <=10) { echo “The number is: $a <br>”; $a++; } ?>
  • 21.
    *Property of STIK0032 Do…while statement 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 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
  • 22.
    *Property of STIK0032 Do…while statement Syntax: Example: Do { Statement to be executed; } while (condition is true); <?php $a = 1; Do { echo “The number is: $a <br>”; $a++; } While ($a <=10); ?>
  • 23.
    *Property of STIK0032 FOR statement executes 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 }
  • 24.
    *Property of STIK0032 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
  • 25.
    *Property of STIK0032 FOR statement Example: Result: <?php for ($a = 1; $a <= 5; $a++) { echo "The number is: $a <br>"; } ?>
  • 26.
    *Property of STIK0032 Function self-contained blocked of codes that perform a specified “function” or task executed by a call to the function can be called anywhere within a page often accepts one or more parameters (“also referred to as arguments”) which you can pass to it
  • 27.
    *Property of STIK0032 Function  Syntax:  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; }
  • 28.
    *Property of STIK0032 Function  Example:  Result: <?php function callMyName() { echo "Francesca Custodio"; } callMyName(); ?>
  • 29.
    *Property of STIK0032 Function Arguments  Function arguments are just like variables  specified right after the function name inside the parentheses  Example: <?php function MyFamName($Fname){ echo "$Fname Garcia. <br>”; } MyFamName("Allen"); MyFamName("Patrick"); MyFamName("Hannah"); ?>
  • 30.
    *Property of STIK0032 Function returns a value Function returns a value using the return statement Example: <?php function 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); ?>
  • 31.
    *Property of STIK0032  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.
  • 32.
    *Property of STIK0032 Arrays PHP array is a special variable which allows you to store multiple values in a single variable $cars = array(“Honda”, “Mazda”, “Mitsubishi”);
  • 33.
    *Property of STIK0032 Arrays The 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] . "."; ?>
  • 34.
    *Property of STIK0032 Types of PHP Arrays Three (3) different types of PHP arrays Indexed array Associative array Multidimensional array
  • 35.
    *Property of STIK0032 Indexed Arrays Indexed 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
  • 36.
    *Property of STIK0032 Indexed Arrays Two 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”;
  • 37.
    *Property of STIK0032 Array count() function The count() function is used to return the length (the number of elements) of an array Example: Result: $fruit = array(“Mango”, ”Santol”, ”Apple”, “Banana”); $arrlength=count($fruit); echo $arrlength;
  • 38.
    *Property of STIK0032 Array: displaying specific content Example: Result: $Fruit = array(“Mango”, ”Santol”, ”Apple”, “Banana”); echo $fruit[1];
  • 39.
    *Property of STIK0032 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>"; } ?>
  • 40.
    *Property of STIK0032 Associative Arrays arrays 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
  • 41.
    *Property of STIK0032 Associative Arrays Two options to create an associative array 1. First: 2. Second: $fruit = array (“Mango”=>”100”, “Santol”=>”200”, “Apple”=>”300”); $fruit [‘Mango’] = “100”; $fruit [‘Santol’] = “200”; $fruit [‘Apple’] = “300”;
  • 42.
    *Property of STIK0032 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"]; ?>
  • 43.
    *Property of STIK0032 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>"; } ?>
  • 44.
    *Property of STIK0032 Multidimensional Arrays an 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
  • 45.
    *Property of STIK0032 Multidimensional Arrays Name QTY Sold Mango 100 96 Apple 60 59 Santol 110 100 <?php $fruit = array ( array("Mango",100,96), array("Apple",60,59), array("Santol",110,100) ); ?>
  • 46.
    *Property of STIK0032 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>";
  • 47.
    *Property of STIK0032 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>"; }