You are on page 1of 16

PHP Operators

Operators
• An operator is a special symbol that
performs a specific operation on operands
and then returns a result.
• Types of operators in PHP:
• Arithmetic operators
• Relational and equality operators
• Logical operators
• Assignment operator
• concatenation operator
© K.S. Mbise Operators Slide 2
Arithmetic operators
• PHP supports various arithmetic
operators for all integer and floating
point numbers.
• Arithmetic operators are used with
numeric values to perform common
arithmetical operations, such as
addition, subtraction, multiplication,
etc.
© K.S. Mbise Operators Slide 3
Arithmetic operators cont…
Operator Use Description
+ $op1 + $op2 Adds $op1 and $op2

- $op1-$op2 Subtracts $op2 from $op1

* $op1*$op2 Multiplies $op1 by $op2


/ $op1 / $op2 Divides $op1 by $op2
% $op1%$op2 Computes the remainder
of dividing $op1 by $op2
© K.S. Mbise Operators Slide 4
Unary operators
• Unary operator can be either
increment or decrement operator.
• The increment operator (++) is used
to increment a value of a variable by 1.
• The decrement operator (--) is used to
decrement a value of a variable by 1.

© K.S. Mbise Operators Slide 5


Unary operators cont…
Operator Use Explanation

++ ++$a Increment $a by 1, then use the new value of


$a in the expression in which $a resides.

++ $a++ Use the current value of $a in the expression


in which $a resides, then increment $a by 1.

-- --$b Decrement $b by 1, then use the new value


of $b in the expression in which $b resides.

-- $b-- Use the current value of $b in the expression


in which $b resides, then decrement $b by 1.

© K.S. Mbise Operators Slide 6


Relational and equality operators
• Relational and equality operators are
used to compare two values and
determine the relationship between
them.
• Conditions in control statements are
formed by using the relational and
equality operators.

© K.S. Mbise Operators Slide 7


Relational and equality operators
cont…
Operator Use Returns true if
> $op1>$op2 $op1 is greater than $op2

>= $op1>=$op2 $op1 is greater than or equal to $op2

< $op1<$op2 $op1 is less than $op2

<= $op1<=$op2 $op1 is less than or equal to $op2

== $op1==$op2 $op1 and $op2 are equal

!= $op1!=$op2 $op1 and $op2 are not equal

© K.S. Mbise Operators Slide 8


Logical operators
• PHP provides logical operators that
may be used to form more complex
conditions by combining simple
conditions.
• Logical operators are used to combine
conditions to test multiple conditions in
the process of making a decision.

© K.S. Mbise Operators Slide 9


Logical operators cont…
operator Use Returns true if
&& $op1 && $op2 If $op1 and $op2 are
both true, the result is
true
|| $op1 || $op2 Either $op1 or $op2 is
true, the result is true
! !$op The result is false if
$op is true.

© K.S. Mbise Operators Slide 10


Assignment operator
• Basic assignment operator (=) is used
to assign a value to a variable
• E.g. $number1 = 5; or $sum = $a +
$b;
• PHP provides several assignment
operators for abbreviating assignment
expressions (shorthand operators).

© K.S. Mbise Operators Slide 11


Assignment operator cont…
• For example, the += operator adds the
value of the expression on the right of
the operator to the value of the variable
on the left of the operator and stores the
result in the variable on the left of the
operator.
• Syntax for abbreviating assignment
expressions: variable operator=
expression;
© K.S. Mbise Operators Slide 12
Assignment operator cont…
Operator Use Equivalent to

+= $op1+=$op2 $op1=$op1+$op2

-= $op1-=$op2 $op1=$op1-$op2

*= $op1*=$op2 $op1=$op1*$op2

/= $op1/=$op2 $op1=$op1/$op2

%= $op1%=$op2 $op1=$op1%$op2

© K.S. Mbise Operators Slide 13


Concatenation operator
• Concatenation is the operation of
joining strings together.
• The concatenation operator is a single
period (.).
• Concatenation operator is specially
designed for strings.
• Treating both operands as strings, it
appends the right operand to the left.
© K.S. Mbise Operators Slide 14
Concatenation operator cont…
• So, "hello"." world" is equivalent to
"hello world"
• echo "The sum of two numbers is
$sum" is equivalent to
• echo "The sum of two numbers is ".
$sum

© K.S. Mbise Operators Slide 15


Thank you for listening

© K.S. Mbise Operators Slide 16

You might also like