SlideShare a Scribd company logo
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>";
}

More Related Content

PPT
Php Operators N Controllers
PPT
Php Calling Operators
PPTX
Php string function
PPT
02 Php Vars Op Control Etc
PDF
PHP7. Game Changer.
PDF
Exceptions in PHP
ODP
PHP Tips for certification - OdW13
ODP
The promise of asynchronous PHP
Php Operators N Controllers
Php Calling Operators
Php string function
02 Php Vars Op Control Etc
PHP7. Game Changer.
Exceptions in PHP
PHP Tips for certification - OdW13
The promise of asynchronous PHP

What's hot (20)

PDF
07 Introduction to PHP #burningkeyboards
PPT
PHP variables
PPTX
Web Application Development using PHP Chapter 2
PDF
Zend Certification Preparation Tutorial
PPT
Introduction to PHP
PPTX
Lecture 2 php basics (1)
PDF
Operators in PHP
PDF
Elegant Ways of Handling PHP Errors and Exceptions
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PPT
Basic PHP
KEY
Intermediate PHP
PPTX
Basic of PHP
PPT
Php i basic chapter 3
PPTX
PHP
PDF
PHP Enums - PHPCon Japan 2021
PPT
My cool new Slideshow!
PPT
slidesharenew1
PDF
Data Types In PHP
PDF
Linux shell script-1
PDF
Introduction to PHP - Basics of PHP
07 Introduction to PHP #burningkeyboards
PHP variables
Web Application Development using PHP Chapter 2
Zend Certification Preparation Tutorial
Introduction to PHP
Lecture 2 php basics (1)
Operators in PHP
Elegant Ways of Handling PHP Errors and Exceptions
PHP - DataType,Variable,Constant,Operators,Array,Include and require
Basic PHP
Intermediate PHP
Basic of PHP
Php i basic chapter 3
PHP
PHP Enums - PHPCon Japan 2021
My cool new Slideshow!
slidesharenew1
Data Types In PHP
Linux shell script-1
Introduction to PHP - Basics of PHP
Ad

Viewers also liked (6)

PPT
Php mysql
PPT
P H P Part I, By Kian
PPTX
PPT
Surprise! It's PHP :) (unabridged)
PPT
Chapter 02 php basic syntax
PDF
10 Webdesign Trends for 2014 by Vanksen
Php mysql
P H P Part I, By Kian
Surprise! It's PHP :) (unabridged)
Chapter 02 php basic syntax
10 Webdesign Trends for 2014 by Vanksen
Ad

Similar to Advanced php (20)

PPTX
Expressions and Operators.pptx
PPT
Introduction to php
PPTX
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PPTX
Learn PHP Basics
PPTX
PHP Basics
PPTX
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PDF
2014 database - course 2 - php
ODP
Advanced Perl Techniques
PPT
Web Technology_10.ppt
PPTX
PHP Basics
PPT
Php Reusing Code And Writing Functions
PPT
PPTX
PPT
Php Chapter 1 Training
PPTX
php programming.pptx
PDF
What's new in PHP 8.0?
PDF
JavaScript for PHP developers
PPTX
Introduction in php part 2
PDF
php AND MYSQL _ppt.pdf
PDF
Php Tutorials for Beginners
Expressions and Operators.pptx
Introduction to php
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
Learn PHP Basics
PHP Basics
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
2014 database - course 2 - php
Advanced Perl Techniques
Web Technology_10.ppt
PHP Basics
Php Reusing Code And Writing Functions
Php Chapter 1 Training
php programming.pptx
What's new in PHP 8.0?
JavaScript for PHP developers
Introduction in php part 2
php AND MYSQL _ppt.pdf
Php Tutorials for Beginners

More from Anne Lee (20)

PDF
Week 17 slides 1 7 multidimensional, parallel, and distributed database
PDF
Data mining
PDF
Data warehousing
PDF
Database backup and recovery
PDF
Database monitoring and performance management
PDF
transportation and assignment models
PDF
Database Security Slide Handout
PDF
Database Security Handout
PDF
Database Security - IG
PDF
03 laboratory exercise 1 - WORKING WITH CTE
PDF
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
PDF
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
DOCX
Indexes - INSTRUCTOR'S GUIDE
PDF
07 ohp slides 1 - INDEXES
PDF
07 ohp slide handout 1 - INDEXES
PDF
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat
PDF
Wk 15 ses 40 42 makrong kasanayan sa pagbabasa
PDF
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita
PDF
Wk 12 ses 32 34 makrong kasanayan sa pakikinig
PDF
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1
Week 17 slides 1 7 multidimensional, parallel, and distributed database
Data mining
Data warehousing
Database backup and recovery
Database monitoring and performance management
transportation and assignment models
Database Security Slide Handout
Database Security Handout
Database Security - IG
03 laboratory exercise 1 - WORKING WITH CTE
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
Indexes - INSTRUCTOR'S GUIDE
07 ohp slides 1 - INDEXES
07 ohp slide handout 1 - INDEXES
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat
Wk 15 ses 40 42 makrong kasanayan sa pagbabasa
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita
Wk 12 ses 32 34 makrong kasanayan sa pakikinig
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1

Recently uploaded (20)

PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPT
tcp ip networks nd ip layering assotred slides
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Digital Literacy And Online Safety on internet
PDF
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
Introuction about ICD -10 and ICD-11 PPT.pptx
Module 1 - Cyber Law and Ethics 101.pptx
Paper PDF World Game (s) Great Redesign.pdf
tcp ip networks nd ip layering assotred slides
Cloud-Scale Log Monitoring _ Datadog.pdf
introduction about ICD -10 & ICD-11 ppt.pptx
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PptxGenJS_Demo_Chart_20250317130215833.pptx
presentation_pfe-universite-molay-seltan.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Digital Literacy And Online Safety on internet
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
Slides PPTX World Game (s) Eco Economic Epochs.pptx
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
WebRTC in SignalWire - troubleshooting media negotiation
SASE Traffic Flow - ZTNA Connector-1.pdf

Advanced php

  • 1. Advanced PHP  PHP Operators  Control Structures  PHP Functions  Arrays
  • 2. *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
  • 3. *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)
  • 4. *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
  • 5. *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.
  • 6. *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.
  • 7. *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"; ?
  • 8. *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.
  • 9. *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!" ?>
  • 10. *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
  • 11. *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
  • 12. *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!"; } ?>
  • 13. *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; }
  • 14. *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!”; } ?>
  • 15. *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; }
  • 16. *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!"; } ?>
  • 17. *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; }
  • 18. *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!"; } ?>
  • 19. *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...; }
  • 20. *Property of STI K0032 While Statement ο‚ Example: <?php $a = 1; While ($a <=10) { echo β€œThe number is: $a <br>”; $a++; } ?>
  • 21. *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
  • 22. *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); ?>
  • 23. *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 }
  • 24. *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
  • 25. *Property of STI K0032 FOR statement ο‚ Example: ο‚ Result: <?php for ($a = 1; $a <= 5; $a++) { echo "The number is: $a <br>"; } ?>
  • 26. *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
  • 27. *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; }
  • 28. *Property of STI K0032 Function ο‚  Example: ο‚  Result: <?php function callMyName() { echo "Francesca Custodio"; } callMyName(); ?>
  • 29. *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"); ?>
  • 30. *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); ?>
  • 31. *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.
  • 32. *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”);
  • 33. *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] . "."; ?>
  • 34. *Property of STI K0032 Types of PHP Arrays ο‚ Three (3) different types of PHP arrays ο‚ Indexed array ο‚ Associative array ο‚ Multidimensional array
  • 35. *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
  • 36. *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”;
  • 37. *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;
  • 38. *Property of STI K0032 Array: displaying specific content ο‚ Example: ο‚ Result: $Fruit = array(β€œMango”, ”Santol”, ”Apple”, β€œBanana”); echo $fruit[1];
  • 39. *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>"; } ?>
  • 40. *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
  • 41. *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”;
  • 42. *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"]; ?>
  • 43. *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>"; } ?>
  • 44. *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
  • 45. *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) ); ?>
  • 46. *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>";
  • 47. *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>"; }