Web Development
Web Development
PHP Operator
• Operator is something that is feed with one or more values.
• PHP operators are characters or set of characters that perform a
special operation within the PHP code.
Arithmetic Operators
• Used to perform simple mathematical operations such as addition,
subtraction, multiplication, division, etc.
Assignment Operators
• PHP assignment operators are used to transfer values to a variable.
Assignment Operators
• PHP assignment operators are used to transfer values to a variable.
Comparison Operators
• Allow you to compare two (2) values.
Increment/Decrement Operators
• PHP 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.
Increment/Decrement Operators
• Example:
Logical Operators
• PHP logical operators are used to combine conditional statements.
String Operators
• There are two (2) string operators.
• Concatenation operator (.) – which 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 Array Operators
• PHP array operators are used to compare arrays.
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.
If statement
• Syntax:
• Example:
• The example will display “Good morning!” if the current time (hour) is
less than 12.
If…Else statement
• This control structure executes 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…Else statement
• Example:
• This example will output “Good morning!” if the current time is less
than 12, otherwise, outputs “Good afternoon!”.
• 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.
ElseIf statement
• A combination of if and else
• It extends an if statement to execute a different statement in case
the original expression evaluates to FALSE.
• Syntax:
ElseIf statement
• Example:
• The example above will output “Good morning!” if the current time is
less than 12, and “Good afternoon!” if the current time is less than 18.
otherwise, it will output “Have a good evening!”.
Switch statement
• The switch statement is similar to a series of if statements on the same
expression.
• Syntax:
Switch statement
• Example:
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 statement
• Example:
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.
Do…While statement
• Syntax:
• Example:
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 statement
• Initialization: it is mostly used to set counter, but can be any code to
be executed once at the beginning of the loop.
• Condition: it is evaluated for each loop iteration. If it evaluates to true,
the loop continues if it evaluates to false the loop ends.
• Increment: mostly used to increment a counter, but can be any code
to be executed at the end of iteration.
For statement
• Example:
• Result:
Reference
• Beginning Web Programming with HTML, XHTML, and CSS
Ducket, J, (2011). John Wiley & Sons