0% found this document useful (0 votes)
3 views

OOP Lesson 2 Operators

The document provides an overview of PHP operators, categorizing them into arithmetic, comparison, logical, assignment, and conditional operators. It includes descriptions and examples for each type of operator, explaining their functions and how they are used in PHP programming. Additionally, it discusses operator precedence and how it affects the evaluation of expressions.

Uploaded by

Abegail Tacogue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

OOP Lesson 2 Operators

The document provides an overview of PHP operators, categorizing them into arithmetic, comparison, logical, assignment, and conditional operators. It includes descriptions and examples for each type of operator, explaining their functions and how they are used in PHP programming. Additionally, it discusses operator precedence and how it affects the evaluation of expressions.

Uploaded by

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

ITELEC3 –

OBJECT ORIENTED
PROGRAMMING (OOP)
Hannah Mae G. Lolong

Davao del Norte State College – Institute of Computing


LESSON 2

PHP OPERATORS

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
PHP Operators
PHP language supports following type of operators.

• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or Ternary) Operators

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Arithmetic Operators
$A = 10, $B = 20
Operators Description Example
+ Adds two operands echo $A + $B; will give 30
- Subtracts second operand from the first echo $A - $B; will give -10
* Multiply both operands echo $A * $B; will give 200
/ Divide numerator by denominator echo $B / $A; will give 2
% Modulus Operator and remainder of after an
integer division echo $B % $A; will give 0

++ Increment operator, increases integer value by


one echo $A++; will give 11

-- Decrement operator, decreases integer value by


one echo $A--; will give 9

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Comparison Operators
$A = 10, $B = 20

Operators Description Example


== Checks if the value of two operands are equal or
not, if yes then condition becomes true var_dump($A == $B); is not true

!= Checks if the value of two operands are equal or


not, if values are not equal then condition var_dump($A != $B); is true
becomes true.
> Checks if the value of left operand is greater than
the value of right operand, if yes then condition var_dump($A > $B); is not true
become true

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Comparison Operators
$A = 10, $B = 20

Operators Description Example


< Checks if the value of left operand is less than
the value of right operand, if yes then condition var_dump($A < $B); is true
becomes true.
>= Checks if the value of left operand is greater than
or equal to the value of right operand, if yes then var_dump($A >= $B); is not true
condition becomes true.
<= Checks if the value of left operand is less than or
equal to the value of right operand, if yes then var_dump($A <= $B); is true
condition becomes true.

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Logical Operators
$A = 10, $B = 20

Operators Description Example


and Called Logical AND operator. If both the
operands are nonzero then condition becomes var_dump($A and $B); is true
true
or Called Logical AND operator. If any of the two
operands are nonzero then condition becomes var_dump($A or $B); is true
true
&& Called Logical AND operator. If both the
operands are nonzero then condition becomes var_dump($A && $B); is true
true

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Logical Operators
$A = 10, $B = 20

Operators Description Example


II Called Logical OR operator. If both the operands
are nonzero then condition becomes true var_dump($A II $B); is true

! Called Logical NOT operator. Use to reverses the


logical state of its operand. If a condition is a true var_dump(!($A && $B)); is false
then Logical NOT operator will make false.

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Assignment Operators

Operators Description Example


= Simple assignment operator, assigns values from echo $C = $A + $B;
right side operands to left side operand will assign value of A+B into C
+= Add AND assignment operator, it adds right
operand to the left operand and assign the result echo $C += $A;
to left operand. is equivalent to C = C + A

-= Subtract AND assignment operator, it subtracts


right operand from the left operand and assign echo $C -= $A;
the result to left operand. is equivalent to C = C - A

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Assignment Operators

Operators Description Example


*= Multiple AND assignment operator, it multiplies
right operand with the left operand and assign the echo $C *= $A;
result to left operand is equivalent to C = C * A

/= Divide AND assignment operator, it divides left


operand with the right operand and assign the echo $C /= $A;
result to left operand is equivalent to C = C / A

%= Modulus AND assignment operator, it takes


modulus using two operands and assign the echo $C %= $A;
result to left operand is equivalent to C = C % A

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Conditional Operator
Conditional operator evaluates first an expression for a true or
false value and then execute one of the two given statements
depending upon the result of the evaluation.

Operators Description Example


?: Conditional expression If condition is true? Then value X:
Otherwise, value Y

<?php
echo (1>0)? "True":"False"; returned value is True
?>

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Operators Categories
All the operators we have discussed above can be categorized 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.

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Precedence of PHP Operators
All the operators we have discussed above can be categorized 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.

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
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.

ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)


Davao del Norte State College – Institute of Computing
Precedence of PHP Operators
Highest precedence appear at the top of the table, those with the
lowest appear at the bottom.
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
ITELEC3 – OBJECT ORIENTED PROGRAMMING (OOP)
Davao del Norte State College – Institute of Computing

You might also like