0% found this document useful (0 votes)
30 views4 pages

Arithmetic Operators: What Is Operator? Simple Answer Can Be Given Using Expression 4 + 5 Is Equal To 9. Here 4 and

Data structure Notes

Uploaded by

Tesfaye Kifle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Arithmetic Operators: What Is Operator? Simple Answer Can Be Given Using Expression 4 + 5 Is Equal To 9. Here 4 and

Data structure Notes

Uploaded by

Tesfaye Kifle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9.

Here 4 and
5 are called operands and + is called operator. PHP language supports following type of
operators.

 Arithmetic Operators
 Comparison Operators
 Logical (or Relational) Operators
 Assignment Operators
 Conditional (or ternary) Operators

Lets have a look on all operators one by one.

Arithmetic Operators
There are following arithmetic operators supported by PHP language −

Assume variable A holds 10 and variable B holds 20 then −

Show Examples

Operator Description Example


+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by one A-- will give 9

Comparison Operators
There are following comparison operators supported by PHP language

Assume variable A holds 10 and variable B holds 20 then −

Show Examples

Operator Description Example


Checks if the value of two operands are equal or not, if yes then (A == B) is not
==
condition becomes true. true.
Checks if the value of two operands are equal or not, if values are not (A != B) is
!=
equal then condition becomes true. true.
Checks if the value of left operand is greater than the value of right (A > B) is not
>
operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than the value of right
< (A < B) is true.
operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to the value (A >= B) is not
>=
of right operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than or equal to the value of (A <= B) is
<=
right operand, if yes then condition becomes true. true.

Logical Operators
There are following logical operators supported by PHP language

Assume variable A holds 10 and variable B holds 20 then −

Show Examples

Operator Description Example


Called Logical AND operator. If both the operands are true then (A and B) is
and
condition becomes true. true.
Called Logical OR Operator. If any of the two operands are non zero (A or B) is
or
then condition becomes true. true.
Called Logical AND operator. If both the operands are non zero then (A && B) is
&&
condition becomes true. true.
Called Logical OR Operator. If any of the two operands are non zero (A || B) is
||
then condition becomes true. true.
Called Logical NOT Operator. Use to reverses the logical state of its
!(A && B) is
! operand. If a condition is true then Logical NOT operator will make
false.
false.

Assignment Operators
There are following assignment operators supported by PHP language −

Show Examples

Operator Description Example


Simple assignment operator, Assigns values from right side C = A + B will assign
=
operands to left side operand value of A + B into C
Add AND assignment operator, It adds right operand to the C += A is equivalent to C
+=
left operand and assign the result to left operand =C+A
Subtract AND assignment operator, It subtracts right
C -= A is equivalent to C
-= operand from the left operand and assign the result to left
=C-A
operand
*= Multiply AND assignment operator, It multiplies right C *= A is equivalent to C
operand with the left operand and assign the result to left =C*A
operand
Divide AND assignment operator, It divides left operand C /= A is equivalent to C
/=
with the right operand and assign the result to left operand =C/A
Modulus AND assignment operator, It takes modulus using C %= A is equivalent to
%=
two operands and assign the result to left operand C=C%A

Conditional Operator
There is one more operator called conditional operator. This first evaluates an expression for a
true or false value and then execute one of the two given statements depending upon the result of
the evaluation. The conditional operator has this syntax −

Show Examples

Operator Description Example


?: Conditional Expression If Condition is true ? Then value X : Otherwise value Y

Operators Categories
All the operators we have discussed above can be categorised into following categories −

 Unary prefix operators, which precede a single operand.


 Binary operators, which take two operands and perform a variety of arithmetic and
logical operations.
 The conditional operator (a ternary operator), which takes three operands and evaluates
either the second or third expression, depending on the evaluation of the first expression.
 Assignment operators, which assign a value to a variable.

Precedence of PHP Operators


Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator −

For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher
precedence than + so it first get multiplied with 3*2 and then adds into 7.

Here operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category Operator Associativity


Unary ! ++ -- Right to left
Multiplicative */% Left to right
Additive +- Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= Right to left

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy