PHP Operator

The PHP language supports basically two types of operators.
1) Unary operator
2) Binary operator

1) Unary Operator:- The Unary Operator works only on single operands.

Increment and Decrement Operator

a) Increment Operator:- There are two types of Operator.

(i) Post-Increment:- It represents ++ sign after variable like $varable++. This is called Post-increment operator. It returns the current value of the variable and increases it by 1.


<?php
$x=100;
echo $x++;
?>
Output:- 100

Try it Yourself

(ii) Pre-Increment:- It represents ++ sign before the variable like ++$varable This is called Pre-increment operator. It increases the value of the variable by 1 and returns the resulting value..


<?php
$x=100;
echo ++$x;
?>
Output:- 101

Try it Yourself

b) Decrement Operator:- There are two types of operator.

Post-Decrement:- It represents — sign after variable like $varable–. This is called Post-Decrement operator. It returns the current value of the variable and decrease it by 1.


<?php
$x=100;
echo $x--;
?>
Output:- 100

Try it Yourself

Pre-Decrement:- It represents — sign before the variable like –$varable This is called Pre-Decrement operator. It decrease the value of the variable by 1 and returns the resulting value.


<?php
$x=100;
echo --$x;
?>
Output:- 99

Try it Yourself

Cast Operator

PHP Language has six cast operators that you can use to force type conversions. You should place the operator on the left-side of the operand.

(array) – This operator converts values into an array.


<?php
$x=100;
$y= (array)$x;
print_r($y);
?>
Output:- Array ( [0] => 100 )

Try it Yourself

(int) or (integer) – This operator convert values into integers.


<?php
$x="100";
$y= (int)$x;
echo $y;
?>
Output:- 100

Try it Yourself

(string) – This operator converts values into a string.


<?php
$x=100;
$y= (string)$x;
echo $y;
?>
Output:- “100”

Try it Yourself

(object) – This operator convert values into the object.


<?php
$x=100;
$y= (object)$x;
print_r($y);
?>
Output:- stdClass Object ( [scalar] => 100 )

Try it Yourself

(real), (float) or (double) – This operator allows you to convert values from any data type into floating-point values.


<?php
$x="100";
$y= (double)$x;
echo gettype($y);
?>
Output:- double

Try it Yourself

(bool) or (boolean) – Use this operator to convert any value into its Boolean form.


<?php
$x="100";
$y= (bool)$x;
echo gettype($y);
?>
Output:- boolean

Try it Yourself

The Negation Operators

PHP language support 2 types of Negation Operators.

a) Logical Negation Operator:- It represents through ! operator. It will give you true if the operand’s value is false. If the value is true, on the other hand, this operator will give you false.


<?php
$x=100;
if(!$x){

echo 'x variable does not exist';

}else{

echo 'x variable exists';

}
?>
Output:- x variable exists

Try it Yourself

b) Bitwise Negation operator:- It represents through “~” operator. It replaces 0 with 1, and vice versa.

2) Binary Operator:- A binary operator requires two Operands.

The Numeric Operators

There are 5 numeric operator.

(i) Addition Operator:- It represents “+” sign between two operands and return the sum value.


<?php
$x=100;
$y=40;
$z=$x+$y;
echo $z;
?>
Output:- 140

Try it Yourself

(ii) Subtract Operator:- It represents “-“ sign between two operands and returns the subtraction value from left operands to right operands.


<?php
$x=100;
$y=40;
$z=$x-$y;
echo $z;
?>
Output:- 60

Try it Yourself

(iii) Multiplication Operator:- It represents “*” sign between two operands and returns the multiply value.


<?php
$x=100;
$y=40;
$z=$x*$y;
echo $z;
?>
Output:- 4000

Try it Yourself

(iv) Division Operator:- It represents the “/” sign between two operands and It divides the value of the left-hand operand by that of the right-hand operand and returns the quotient.


<?php
$x=100;
$y=40;
$z=$x/$y;
echo $z;
?>
Output:- 2.5

Try it Yourself

(v) Modulus Operator:- It represents the “%” sign between two operands and divides the value of the left-hand operand by that of the right-hand operand and returns the remainder.


<?php
$x=100;
$y=40;
$z=$x%$y;
echo $z;
?>
Output:- 20

Try it Yourself

Assignment Operator

An assignment operator allows you to assign values to your variables. Assignment operators are used with numeric values.

= operator:- It is used to assign the value.


x=2;

+= operator:- it is basically used to addition between two operands.


x+=y like x=x+y;

Try it Yourself

-= operator:- it is basically used to subtraction between two operands.


x-=y like x=x-y;

Try it Yourself

*= operator:- it is basically used to multiplication between two operands.


x*=y like x=x*y;

Try it Yourself

/= operator:- it is basically used to division between two operands.


x/=y like x=x/y;

Try it Yourself

%= operator:- it is basically used to modulas between two operands.


x%=y like x=x%y;

Try it Yourself

Comparison Operator

You can use these operators to compare the values of two operands.

“==” Operator – This operator checks whether the operands have equal values. It will give you true if the values are equal.

example,


$x == $y

Try it Yourself

“!=” Operator – if the value of the operand are not equal. For instance,


100 != 40

evaluates to true.
Try it Yourself

“>” Operator – this refers to greater than the operator. you can check whether the value of the left-hand operand is greater than that of the right-hand operand.
Try it Yourself

“<" Operator - this refers to less-than operator. you can check whether the value of the left-hand operand is less than that of the right-hand operand.
Try it Yourself

“<=" Operator - this refers to less than or equal operator. It will give you true if the value of the left-hand operand is less than or equal to the second operand.

Try it Yourself

“>=” Operator – this refers to greater than or equal operator. It will give you true if the value of the left-hand operand is greater than or equal to the second operand.

example, $x >= $y
Try it Yourself

Logical Operator

PHP represents some Logical operator.

(i) Logical AND:- this represents through “&&”. It will give you true if the both operands are true.


$x && $y

Try it Yourself

(ii) Logical OR:- this represents through “||”. It will give you true if the one operands is true.


$x || $y 

Try it Yourself

(iii) Logical XOR:- this represents through “xor”. It will give you true if only one of the operands is true. Thus, you will get false if both of the operands are true or false.
Try it Yourself

Concatenation Operator

this operator is used to concate the two or more strings. It works only for strings value.
It converts non-string operands to string operands.


<?php
$emp_age = 23;
print "Employee age is " . $emp_age;
?>
Employee age is 23

Try it Yourself

PHP Conditional Assignment Operator

Ternary Operator(?:)- this operator is used to check conditions.

Syntax:-


conditional_statement ? first_expression : second_expression

The “?” operator evaluates the result of “conditional_statement” If the result is true, the
operator will return the value of “first_expression”. If the result is false, the operator will
give you the value of “second_expression”.

Example:-


<?php
$x=10;
echo ($x==10) ? 'even number': 'odd number';
?>
Output:- even number

Try it Yourself

coalescing operator (??)- PHP7 introduce coalescing operator. The Null coalescing operator returns its first operand if it exists and is not NULL otherwise, it returns its second operand.


<?php
$employee='John';
$employee_name = $employee ?? 'employee not exists';
/** This is equivalent to: **/
$employee_name = isset($employee) ? $employee : 'employee not exists';
?>
Output:- John

Try it Yourself