Applications Development and
Emerging Technologies
MODULE 2
PHP Operators and Control
Structures
PHP Operators and Control Structures
Objectives
• To understand the different types of operators that are
available on PHP.
• To know what is operator precedence and operator
associativity in PHP.
• To ensure that programs expression are logically correct by
using the correct referencing values of its precedence.
• To use escape sequence properly in the program.
• To know the different approach of control structures.
• To know the fundamentals syntax for conditional and
looping structures.
• To properly use the compound expression using the logical
operators.
• To know the rules of break, continue, and goto statements.
• To see some alternative enclosure for control structures.
PHP Operators and Control Structures
Operators
An operator is a symbol that specifies a particular action in an expression
Operator Precedence, Associativity, and Pupose
PHP Operators and Control Structures
Operator Precedence, Associativity, and Pupose
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.124
PHP Operators and Control Structures
Operator Precedence
is a characteristic of operators that determines the order in which they
evaluate the operands surrounding them.
Output:
PHP Operators and Control Structures
Operator Associativity
is a characteristic of an operator specifies how operations of the same
precedence are evaluated as they are executed.
Output:
Example:
PHP Operators and Control Structures
Arithmetic Operator
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.124
Assignment Operator
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.126
PHP Operators and Control Structures
Increment and Decrement Operator
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.127
Example: Output:
PHP Operators and Control Structures
Comparison Operator
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.129
Example: Output:
PHP Operators and Control Structures
Logical Operator
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.128
Equality Operator
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.129
PHP Operators and Control Structures
Bitwise Operator
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.130
Example: Output:
PHP Operators and Control Structures
Escape Sequences
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.132
PHP Operators and Control Structures
PHP Control Structures: Conditional Statements
if statement
syntax: elseif statement
if(expression) { syntax:
statement… if(expression) {
} statement…
}
else statement elseif(expression) {
syntax: statement…
if(expression) { } else {
statement… statement…
} else { }
statement
}
PHP Operators and Control Structures
PHP Control Structures: Conditional Statements
Example:
Output:
PHP Operators and Control Structures
PHP Control Structures: Conditional Statements
Nested if…else
Output:
Example:
PHP Operators and Control Structures
PHP Control Structures: Conditional Statements
Compound expression using logical operators
Output:
Example:
PHP Operators and Control Structures
PHP Control Structures: Conditional Statements
switch statement
syntax:
switch($category){
case opt1:
statement…
break;
case opt2:
statement…
break;
case opt3:
statement…
break;
…
default:
statement…
}
PHP Operators and Control Structures
PHP Control Structures: Conditional Statements
Switch Statement
Example: Output:
PHP Operators and Control Structures
PHP Control Structures: Looping Statements
while statement for statement
syntax: syntax:
while(expression){ for(expr1;expr2;expr3)
statement… {
} statement…
}
do … while statement
syntax:
do{
statement…
}while(expression);
PHP Operators and Control Structures
PHP Control Structures: Looping Statements
Example: Output:
PHP Operators and Control Structures
PHP Control Structures: break, continue,
and goto statement
break
break statement is placed within the code of a loop to cause
the program to break out of the loop statement.
continue
continue statement causes execution of the current loop
iteration to end and commence at the beginning of the next iteration.
goto … label:
goto statement is used to jump to other section of the
program to support labels.
PHP Operators and Control Structures
PHP Control Structures: break, continue,
and goto statement
Example:
Output:
PHP Operators and Control Structures
PHP Control Structures: alternative enclosure syntax
Involves replacing the opening bracket with a colon(:) and
replacing the closing bracket with endif;, endwhile;,endfor;,
endswitch
Example: Output:
PHP Operators and Control Structures
Summary
• Simplifying expression needs to follow a general precedence set by
PHP scripts.
• An operator specifies a particular action in a given expression.
• Available PHP operators are arithmetic, conditional, assignment,
logical and bitwise operator.
• Operator precedence characteristics determines the order in which
they evaluate the operands surrounding them.
• Operator associativity characteristic specifies how operations of the
same precedence are evaluated as they are executed.
• Post increment or decrement always execute the statement first
before incrementing or decrementing the value of a given variable.
• Pre increment or decrement always increment or decrement its
variable value before executing the statement.
• Escape sequence has its own special task in executing the string
as an output.
PHP Operators and Control Structures
Summary (Continue)
• Conditional statements are statement the will execute statement
inside a block if a given condition is true
• Use compound expression to minimize the process of coding using
nested if statement.
• break statement caused the program to break if used.
• continue statement end the current loop and continue to next
iteration.
• goto statement used to jump to other section of the program.
• Alternative enclosure syntax is available for if, while, for, foreach,
and switch control.