This document discusses operators, expressions, and control statements in PHP. It covers arithmetic, logical, relational, bitwise, and assignment operators. It describes expressions and how they are evaluated in PHP. It also explains control structures like if/elseif/else statements, switch statements, while loops, do-while loops, and how to use break and continue statements. Examples are provided for each concept.
DevoxxFR 2024 Reproducible Builds with Apache Maven
PHP Operators, Expressions, Control Statements and Loops
1. hasen@microcis.net July 07, 2013Hassen poreya
Trainer, Cresco Solution
Afghanistan Workforce
Development Program
PHP
Operators, Expressions, Control Statement, Loops
2. Operators
A symbol that tells PHP to perform a mathematical
or logical operation.
Arithmetic
Logical
Bitwise
Miscellaneous
4. Using a string with an Arithmetic Operator
<?php
$foo = "0"; // $foo is string
$foo = $foo + 2; // $foo is now an integer: 2
$foo = $foo + 1.3; // $foo is now a float: 3.3
$foo = 5 + "10 Books"; // $foo is integer: 15
?>
5. Using a string with an Arithmetic Operator
++ increment
-- decrement
If an increment operator precedes the variable, the
variable will be incremented prior to evaluation of the
expression; otherwise, the variable isn’t operated on
until after the value of the expression is computed.
<?php
$count=1;
print($count++); //prints 1, but after print,
count is 2
print(++$count); //prints 3
?>
6. Logical and Relational Operators
Relational operators compare values and return either TRUE
or FALSE.
Logical operators perform logical operations on TRUE and
FALSE
16. Expressions
A bit of PHP that can be evaluated to produce a
value.
PHP is an expression-oriented language
Everything is an expression
Expressions are combinations of identifiers and
operators.
17. Expressions
Generally executed from left to right
Some operators are processed before others.
Use parentheses to force an operation to occur
first
Two general rules
Some operators work only on certain data types.
If the operation is on a mix of an integer and a double,
the integer will be converted to a double.
18. Expressions
PHP will even read doubles with an exponent.
<?php
print((1 + "1") . "<BR>");
print((1 + " 2") . "<BR>");
print((1 + "3extra stuff") . "<BR>");
print((1 + "4.5e6") . "<BR>");
print((1 + "a7") . "<BR>");
?>
19. Evaluation
PHP will even read doubles with an exponent.
<?php
print((1 + "1") . "<BR>"); //1 + 1 == 2
print((1 + " 2") . "<BR>"); //1 + 2 == 3
print((1 + "3extra stuff") . "<BR>"); //1 + 3 == 4
print((1 + "4.5e6") . "<BR>"); //1 + 4500000 == 4500001
print((1 + "a7") . "<BR>"); //1 + 0 == 1
//“a7” begins with a letter, PHP treats it as zero!
?>
21. TRUE and FALSE
Zero (0 and 0.0) and an empty string ("") are
considered to be FALSE.
Any other numerical value or string is TRUE.
Some control statements expect a Boolean value.
23. If, ifelse, else Statements
<?php
if ($month == "January") {
$num_month = 1;
}
elseif($month == "February")
$num_month = 2;
// March to October omitted here
elseif($month == "November")
$num_month = 11;
else
$num_month = 12;
?>
24. switch Statements
Switch(expression){
case case-expression
expression;
break;
case case-expression
expression;
break
…
default
expression;
}
The expression inside the
switch statement is evaluated
and then compared to each
expression following a case
expression.
A default statement works
exactly like an else
statement; it matches when
no other case matches.
Break statement is used
escape from the switch
statement.
Syntax Description
25. switch Statement
<?php
switch($month) {
case "January":
$num_month = 1;
break;
// February to October omitted here
case "November":
$num_month = 11;
break;
default: // It must be December
$num_month = 12;
}
?>
26. Loops
Loops allow you to repeat lines of code based on
some condition.
Example
Read lines from a file until the end is reached.
Print some text a certain number of times.
27. while Loop Statement
While (condition/expression){
statement;
}
It is useful when you are not sure how many times
you will need to iterate your statements.
30. do while Statement
<?php
$total = 0;
$i = 1;
do {
$total += $i++;
}
while ($i <= 10);
echo "The sum of 1 to 10 is : "
. $total;
?>
31. break Statement
When a break statement is encountered, the
execution jumps to the outside of the loop or
switch statement.
<?php
while (true){
echo “This line is printed here!”;
break;
echo “This line would never be printed”;
}
//These codes will be executed!
?>
32. break Statement
<?php
$i=0; $j=0;
while ($i<10){
while($j<5){
if($j==5)
break; // the loop is stopped
$j++; // once $j reaches 5
}
$i++;
}
echo „$i=‟.$i“<br>”;
echo „$j=‟.$i;
?>
33. continue Statement
Similar to the break statement
But only the current execution of the loop is
stopped, after that loop is continued.
<?php
while ($i++<5){
if($i==2)
continue;
echo $i; //2 will not be printed.
}
?>
35. Class exercise
Write a while loop that outputs the even numbers
between 1 and 100.
Rewrite the script for Problem 1 using do while
loop.
Write a while loop that outputs the numbers from
100 to 0 by 10's.