KEMBAR78
PHP - Operators | PDF | Boolean Data Type | Computer Programming
0% found this document useful (0 votes)
7 views44 pages

PHP - Operators

The document provides an overview of various types of operators used in programming, including arithmetic, assignment, bitwise, comparison, logical, increment/decrement, string, array, conditional, null coalescing, and type operators. Each operator type is explained with its purpose, syntax, and examples. Additionally, it covers operator precedence and associativity, which dictate the order of operations in expressions.

Uploaded by

nevithasenthil25
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)
7 views44 pages

PHP - Operators

The document provides an overview of various types of operators used in programming, including arithmetic, assignment, bitwise, comparison, logical, increment/decrement, string, array, conditional, null coalescing, and type operators. Each operator type is explained with its purpose, syntax, and examples. Additionally, it covers operator precedence and associativity, which dictate the order of operations in expressions.

Uploaded by

nevithasenthil25
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/ 44

OPERATORS

• Symbols that are used when an operation is performed on variables or constants.


• Types:
- Arithmetic
- Assignment
- Bitwise
- Comparison
- Logical
- Increment or Decrement
- String
- Array
- Conditional
- Null Coalescing
- Type
Arithmetic Operator:
• These operators are used in mathematical expressions
• Operators: +, - , * , / , % , **(Exponentiation)
Assignment Operators:
• Used to assign a value to a variable.
• 2 types:
- Basic Assignment Operator
- Combined Assignment Operator
Basic Assignment Operator
- Denoted by = symbol
- Used to assign a value to a variable.
- syntax: $variable=value;

- Use assignment operator for multiple assignments.


- syntax: $var1=$var2=$var3=value;
Combined Assignment Operator / Short hand assignment operators
• Combination of two operators
• First specifies operation to be performed and second is assignment
operator
• Operators: += , -= , *=, /+ , %= , **=
• Ex: $a=2;
$a+=4; (2+4=6 will be assigned to a)
Bitwise Operator:
• Operates on single bits of an operand.
• Mostly applied in integer type but cam also be applied to string type.
• Operators: & , | , ~ , ^ , << , >>
The Bitwise AND(&) Operator:
• Both operands are 1, the & operator produces 1 as result.
• If any one operand is 0, the & operator produces 0 as result.
• Syntax: $operand1 & $operand2;
• Ex: 00010111 (23)
00001111 (15)
-----------
00000111
------------ (7)
The Bitwise OR(|) Operator:
• If any one or both operands is 1, the | operator produces 1 as result.
• If both are 0, the result will be 0;
• Syntax: $operand1 | $operand2;
• Ex: 00010111 (23)
00001111 (15)
-----------
00011111 (31)
------------
COMPARISON OPERATORS
• Determines relationship between two values assigned to the variables
• Outcome: Boolean value (T / F)
• Spaceship Operator: Returns an integer value between -1 to 1.
The Equal(==) Operator
• Compares the equality between the values of two variables.
• Returns true is both variables are equal or same. Otherwise, returns false.
The Identical (===) Operator
• Compares the equality between the values of two variables including data
type
• Returns true if both value and data type of two variables are same.
Otherwise false.
The Not equal (!= and <>) Operators
• Opposite of equal operators
• Compares inequality between the values of two variables.
• Returns true if the value of two variables is not same. Otherwise false.
The Non-Identical (!==) Operator
• Compares the inequality between the values of two variables including data
type.
• Returns true if the value or the data type of the two variables is different.
Otherwise false.
The Spaceship < = > Operator
• Compares values of two variables but not their data types.
• Also compares two numbers or strings directly without assigning them to the
variable.
• Returns 0 if the values of the two variables are equal or same.
• Return 1 if the value on the left side of < = >operator is greater than on the right
side and will return -1 if the value on right side of < = > operator is greater than
on the left side.
LOGICAL OPERATORS
• Compare two or more relational expressions in a single statement
• Outcome : Boolean (T / F)
• Operators: and, or, xor, &&, ||, !
The Logical AND (and / &&)Operators
• Compares two or more comparison expressions
• Returns true if outcome of all the given expressions are true, otherwise it will
return false.
• Mostly used in control structures in which final outcome is based on outcome
of two or more than two conditions.
• Synatx:
comp_exp and comp_exp
comp_exp && comp_exp
• Ex:
$var1==$var2 && $var3==$var4
The Logical OR (or / ||)Operators
• Compares two or more comparison expressions
• Returns true if outcome of any one of given expressions is true, otherwise it
will return false.
• Mostly used in control flow statements in which final outcome is based on
outcome of two or more than two conditions.
• Syntax:
comp_exp or comp_exp
comp_exp || comp_exp
• Ex:
$var1==$var2 || $var3==$var4
The Logical XOR (xor)Operators
• Compares two or more comparison expressions
• Returns true if outcome of any one of given expressions is true.
• If the outcome of Mostly used in control flow statements in which final
outcome is based on outcome of two or more than two conditions.
• Syntax:
comp_exp xor comp_exp

• Ex:
$var1==$var2 xor $var3==$var4
The Logical NOT (!)Operators
• Checks whether a given comparison expression returns true or false.
• Returns true if outcome of given expressions is false. Other wise it
returns false.
• It inverts the outcome of comparison operator.
• Mostly used in control flow statements.
• Syntax:
!(comp_exp)

• Ex:
!($var1==$var2)
The Increment (++) or Decrement (--) Operators
• Increment (++) :Used to increase the value of its operand by 1
• Decrement (--): Used to decrease the value of its operand by 1
• 2 Notations
- Postfix
- Prefix
The Postfix Notation:
• Increment or decrement operator is used after the variable.
• Syntax:
• $var1++; (increment)
• $var1--; (decrement)
First the value in the variable is returned and then it will be increased or decreased by 1.
• Ex: $y=$x--;
or
$y=$x;
$x=$x-1;
The Prefix Notation
• Increment or decrement operator is used before the variable.
• Syntax:
++$var1; (increment)
--$var1; (decrement)
First the value in the variable is incremented or decremented by 1 and then it will be assigned
to the variable.
• Ex: $y=--$x;
or
$x=$x-1;
$y=$x;
The String Operators
• 2 operators are used only on string type data
• Operators & Operations
. Concatenation
.= Concatenation assignment
Concatenation Assignment Operator
• Joins two or more strings stored in different variables
• First concatenation is performed on two variables and then the resultant is
assigned back to first variable.
The Array Operators:
• Similar to comparison operators
• The functionality of these operators changes slightly while working with
arrays.
• Operators: +, ==, ===, !=, <>, !==

The union (+) operator


• Works as an union operator
• Joins two arrays and returns all the common and unique values in the arrays.
• The values that exists in both arrays are not repeated.
The Equality (==) operator
• Checks the equality between the values of two arrays
• Returns true if two arrays are same or else false.

The Identity (===) operator


• Checks the equality between the values of two arrays including their
order,index and data type
• Returns true if two order, index, data type of arrays are same or else false.
The Inequality (!= / <>) operator
• Checks the inequality between the values of two arrays.
• Returns true if values of two arrays are not same or else false.

The Non-Identity (!==) operator


• Checks the inequality between the values of two arrays including their
order,index and data type
• Returns true if two order, index, data type of arrays are different or else false
The conditional (?) operator
• Also known as ternary operator
• Works on three expressions.
• If first expression is true, returns the second.
• If first expression is false, returns third expression.
• It is a short syntax for if else statement.
• Syntax:
Condn_exp ? exp1 : exp2 ;
• Short hand syntax:
Condn_exp ? : exp2;
Null Coalescing (??) operator

• To overcome the drawback of conditional operator

• Checks if a value is assigned to a variable or not and also if the assigned


value is NULL or not.

• This operator performs inline comparison and will return first expression if a
value associated with the first expression does exist and is not NULL.

• Otherwise it returns second expression or string.


• Syntax:

exp1 ?? exp2;

If the exp1 is not NULL and has a value, it will return exp1. Otherwise, it will
return exp2.

• Syntax similar to conditional operator

exp1 ? exp1 : exp2;

• Nesting is also possible

exp1 ?? exp2 ?? exp3 ?? expn ;


Operator Precedence

• Determines the order of execution of operators

• An operator with a high precedence is used before an operator with a low


precedence.

• Ex:

$x = $a + $b * $c ;
Operator Associativity

When two or more operators have same precedence in an expression,


the order in which the operation is performed – Operator Associativity.

2 Types

- Left to Right

- Right to Left
Left to Right Associativity

• All operations are performed from left to right

• Ex: $x = $a + $b +$c;

a and b are first added and the result is then added to c.

Right to Left Associativity

• All operations are performed from right to left

• Ex: X + = Y ; / x = x + y;

Y value is added to x first.

You might also like