PHP operator
Operators are generally used to operate on values. Following are the types of operator’s used in PHP.
Arithmetic operators:+ - Addition Example : x=2, y=4 ; z= x+y
- - subtraction Example: x=2, y=4 ; z= x-y
* - Multiplication Example: x=2, y= 4; z= x*y
/ - Division Example: x=2, y= 4; z= x/y
% - Modulus Example: x=2, y= 4; z= x%y
++ - Increment Example: x=2 ; x ++ -> 3
-- - Decrement Example : x=2; x-- ->1
Assignment operators:= - Example : x=y
+= - is the same as x=x+y
-= - is the same as x=x-y
Same is applicable to - /, *, %
Comparison operators:== - Equals to. Example: 5==2 returns false
!= - Not Equals to. Example: 5!=2 returns true
> - Greater than
< - Less than
>= - Greater than or equal to
<= - Less than or equal to
Logical Operators:&& - AND . Example – x=1, y=2; (x<10 && y>1) returns true
|| - AND . Example – x=1, y=2; (x<10 || y>1) returns true
! - NOT . Example – x=1, y=2; !(x==y) returns true
Brief note on comparing object with == and === with examples.
Using == compares the “content” of the objects while === compares the “handles” of the object. For example: When a clone of an object is created; using == will return true because of same values while === will return false because of difference in handle.
<?php
Class sample {}
$original = new sample();
$copy1 = clone $original;
Print{int} ($original == $copy1} . “\n”;
Print{int} ($original === $copy1}. “\n”;< BR> ?>
Explain the ternary conditional operator in PHP?
The ternary conditional operator (?:) is used for evaluation of a statement.
Example:$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
The statement above can be considered as an if else statement. The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.