PHP Operators
Session 5
Review - I
An identifier is use to refer to the memory location
with the help of any variable
A data type describes the type of data a variable will
store. The different data types are:
Integer numbers
Floating numbers
Strings
Boolean
PHP / Session 5 / Slide 2 of 27
Review - II
Numeric data or text enclosed in the quotes is considered to
be a string data type
Variable:
Name assigned to a memory location where the data is
stored
Enables to retrieve data from the memory location
Dollar sign ‘$’ is placed before a variable name
Variable created without any value assigned to it then
that variable takes its value as NULL
Equal to sign ‘=’ is used to assign a value to the varaible
PHP / Session 5 / Slide 3 of 27
Review - III
Local variable:
Declared and used inside a function
Lifetime remains till the function terminates
Global variable:
Uses its value throughout the page
Declared outside the function
Called in the program with the help of the keyword global
Static variable:
Local variable made static with the help of the keyword
static
Retains its value even after the function terminates
PHP / Session 5 / Slide 4 of 27
Review - IV
Environment variable:
System-defined variable
Provides information about the transactions held
between the client and the server
Deals with the action of request and response
those are held in between the server and the client
PHP / Session 5 / Slide 5 of 27
Objectives
Use Arithmetic operators
Use Logical operators
Use Relational operators
Use Bitwise operators
Use Assignment operators
Use String operators
Use Increment and Decrement Operators
PHP / Session 5 / Slide 6 of 27
Operators
Operators are pre-defined symbols that allow
to perform specific actions
An expression contains operators and operands
The operators are assigned precedence values
This precedence value indicates the order in
which the operators are evaluated in an
expression
PHP / Session 5 / Slide 7 of 27
Arithmetic Operators - I
Arithmetic Operators are used to perform operations
on numbers
We use the BODMAS rule to solve precedence
between the arithmetic operators.
Brackets are used to force precedence of operators
PHP / Session 5 / Slide 8 of 27
Arithmetic Operators - II
Following table lists the different arithmetic operators in PHP:
Operator
Name
Description
+
Addition
Adds the operands
-
Subtraction
Subtracts the second operand from the first
operand
*
Multiplication Multiplies operands with each other
/
Division
Divides the first operand by the second operand
%
Modulus
Returns the remainder when the first operand is
divided by the second operand
PHP / Session 5 / Slide 9 of 27
Relational Operators - I
Following table lists the different relational operators
in PHP
Operator
Name
Description
==
Equal to
Returns true if both the operands are equal
!=
Not equal to
Returns true if the first operand is not
equal to the second operand
<
Less than
Returns true if the first operand is less
than the second operand
PHP / Session 5 / Slide 10 of 27
Relational Operators - II
Operator
Name
Description
<=
Less than or equal to
Returns true if the first operand is
less than or equal to the second
operand
>
Greater than
Returns true if the first operand is
greater than the second operand
>=
Greater than or equal to
Returns true if the first operand is
greater than or equal to the second
operand
PHP / Session 5 / Slide 11 of 27
String Operators
String operators operate on character data
Following table lists the different string operators in PHP
Operator
Name
Description
.
Concatenation
Returns a concatenated string
.=
Concatenating assignment Appends the argument on the right
side to the variable
PHP / Session 5 / Slide 12 of 27
Logical Operators
Logical operators enable us to combine two
or more test expression in a condition
Evaluate expressions and return a Boolean
value
PHP / Session 5 / Slide 13 of 27
Logical Operators - I
PHP provides us with the following logical
operators:
Operator
General Form
Description
&&
Expression1&& Expression2 Returns true only if both the
expressions are true
||
Expression1 || Expression2
Returns true if any one of
the expression is true
!
!Expression
Returns true only if the condition
is not true
PHP / Session 5 / Slide 14 of 27
Logical Operators - II
Operator
General Form
Description
AND
Expression1 AND Expression2
Returns true only if all the
expressions are true
OR
Expression1 OR Expression2
Returns true if any one of the
expression is true
XOR
Expression1 XOR Expression2
Returns false if both Expression1
and Expression2 are true or if both
Expression1 and Expression2 are
false and true otherwise.
PHP / Session 5 / Slide 15 of 27
Bitwise Operators - I
Operate on the bits of an operand
Work on small-scale binary representation of
data
PHP / Session 5 / Slide 16 of 27
Bitwise Operators - II
Following table lists the different bitwise operators in PHP:
Operator
Name
General Form
Description
&
AND
Operand1
Operand2
& Sets to 1 if both the bits of both the
operands are 1 and 0 otherwise
|
OR
Operand1 | Operand2
Sets to 1 if either of the bits of the
operands are 1 and 0 otherwise
PHP / Session 5 / Slide 17 of 27
Bitwise Operators - III
Operator
Name
General Form
^
EXCLUSIVE-OR Operand1
Operand2
~
COMPLEMENT
~ Operand
Description
^ Compares two bits and
sets the bit to 1 if the bits
are different and 0
otherwise
Compares and sets the bits
that are not set and 0
otherwise
PHP / Session 5 / Slide 18 of 27
Bitwise Operators - IV
Operator
Name
General Form
Description
<<
SHIFT
LEFT
Operand1 << Operand2
Shifts the bits of
Operand1,
Operand2
times to the left
>>
SHIFT
RIGHT
Operand1 >> Operand2
Shifts the bits of
Operand1,
Operand2
times to the right
PHP / Session 5 / Slide 19 of 27
Assignment Operator
Enables us to set the operand on the left
side to the value of the expression on the
right side
‘=’ sign is the assignment operator
Different from the equal to ‘= ‘sign used as
the relational operator
PHP / Session 5 / Slide 20 of 27
Increment and Decrement
Operators - I
Increment operators increase the value of the
operand by one
Decrement operators decrease the value of the
operand by one
These operators function more effectively
within loops such as for, do while, and while
PHP / Session 5 / Slide 21 of 27
Increment and Decrement
Operators - II
Following table lists the different increment and decrement operators in
PHP:
Operand
Operator Name
Description
++$a
Pre-increment
Increments the operand value by one and
then returns this new value to the variable
$a++
Post-increment
Returns the value to the variable and then
increments the operand by one
PHP / Session 5 / Slide 22 of 27
Increment and Decrement
Operators - III
Operand Operator Name Description
--$a
Pre- decrement
Decrements the operand by one and then
returns this new value to the variable
$a--
Post decrement
Returns the value to the variable and then
decrements the operand by one
PHP / Session 5 / Slide 23 of 27
Precedence of Operators
Following table lists illustrates the precedence of operators in PHP
Precedence
Highest Precedence
Operators
*, /, %
+, - , .
<, <=, >, >=
&&
||
AND
XOR
Lowest Precedence
OR
PHP / Session 5 / Slide 24 of 27
Summary - I
An Operator is any symbol that performs an
operation on an operand. An operator enables us to
work on variables, strings, and numbers.
Operators enable to control the program flow
The types of operators are:
Assignment Operators
Arithmetic operators
String Operators
Logical Operators
Incremental and
Relational Operators
Decremental Operators
Bitwise Operators
PHP / Session 5 / Slide 25 of 27