PHP-Operators

We have learned in the last tutorial, about Reference Variable in PHP. Now, we will discuss operators in PHP.

PHP Operators:

Simple answer can be given using expression 2+ 5 is equal to 7. Here 2 and 5 are called operands and + is called operator. PHP language supports following type of operators.

Types of Operators:

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Incrementing and Decrementing Operators
  • Conditional (or ternary) Operators

 Arithmetic Operators:

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here’s a complete list of PHP’s arithmetic operators.

Operator
Description
Example
Result
+Addition$x + $ySum of $x and $y.
Subtraction$x – $yDifference of $x and $y.
*Multiplication$x * $yProduct of $x and $y.
/Division$x / $yQuotient of $x and $y.
%Modulus$x % $yRemainder of $x divided by $y.

 

Example:

<?php
$x = 10;
$y = 4;
echo($x + $y);  // 0utputs: 14
echo($x - $y);   // 0utputs: 6
echo($x * $y);  // 0utputs: 40
echo($x / $y);   // 0utputs: 2.5
echo($x % $y);   // 0utputs: 2
?>

 

Comparison Operators:

The comparison operators are used to compare two values in a Boolean form.

 

OperatorNameExampleResult
==Equal$x == $yTrue if $x is equal to $y
===Identical$x === $yTrue if $x is equal to $y, and they are of the same type
!=Not equal$x != $yTrue if $x is not equal to $y
<>Not equal$x <> $yTrue if $x is not equal to $y
!==Not identical$x !== $yTrue if $x is not equal to $y, or they are not of the same type
<Less than$x < $yTrue if $x is less than $y
>Greater than$x > $yTrue if $x is greater than $y
>=Greater than or equal to$x >= $yTrue if $x is greater than or equal to $y
<=Less than or equal to$x <= $yTrue if $x is less than or equal to $y

 

Example:

<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z);        // Outputs: boolean true
var_dump($x === $z);     // Outputs: boolean false
var_dump($x != $y);        // Outputs: boolean true
var_dump($x !== $z);     // Outputs: boolean true
var_dump($x < $y);        // Outputs: boolean true
var_dump($x > $y);       // Outputs: boolean false
var_dump($x <= $y);   // Outputs: boolean true
var_dump($x >= $y);   // Outputs: boolean false
?>

| Also Read | Introduction To PHP

 

Logical Operators:

The logical operators are typically used to combine conditional statements.

OperatorNameExampleResult
andAnd$x and $yTrue if both $x and $y are true
orOr$x or $yTrue if either $x or $y is true
xorXor$x xor $yTrue if either $x or $y is true, but not both
&&And$x && $yTrue if both $x and $y are true
||Or$x || $yTrue if either $$x or $y is true
!Not!$xTrue if $x is not true

 

Example

<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
echo "$year is a leap year.";
} else{
echo "$year is not a leap year.";
}
?>

 

Assignment Operators:

The assignment operators are used to assign values to variables.

OperatorDescriptionExampleIs The Same As
=Assign$x = $y$x = $y
+=Add and assign$x += $y$x = $x + $y
-=Subtract and assign$x -= $y$x = $x – $y
*=Multiply and assign$x *= $y$x = $x * $y
/=Divide and assign quotient$x /= $y$x = $x / $y
%=Divide and assign modulus$x %= $y$x = $x % $y

Example:

<?php
$x = 15;
echo $x;  // Outputs: 15

$x = 15;
$x += 30;
echo $x;  // Outputs: 45

$x = 30;
$x -= 20;
echo $x;  // Outputs: 10

$x = 5;
$x *= 20;
echo $x;  // Outputs: 100

$x = 100;
$x /= 20;
echo $x;  // Outputs: 5

$x = 100;
$x %= 15;
echo $x;  // Outputs: 10
?>

Incrementing and Decrementing Operators:

OperatorNameEffect
++$xPre-incrementIncrements $x by one, then returns $x
$x++Post-incrementReturns $x, then increments $x by one
–$xPre-decrementDecrements $x by one, then returns $x
$x–Post-decrementReturns $x, then decrements $x by one

 

Example

<?php
$x = 5;
echo ++$x; // Outputs: 6
echo $x; // Outputs: 6

$x = 10;
echo $x++; // Outputs: 10
echo $x; // Outputs: 11

$x = 15;
echo --$x; // Outputs: 14
echo $x; // Outputs: 14

$x = 10;
echo $x--; // Outputs: 10
echo $x; // Outputs: 9
?>

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.

OperatorDescriptionExample
? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y

 

Example

<?php
$a = 10;
$b = 20;

/* If condition is true then assign a to result otheriwse b */
$result = ($a > $b ) ? $a :$b;

echo "TEST1 : Value of result is $result<br/>";

/* If condition is true then assign a to result otheriwse b */
$result = ($a < $b ) ? $a :$b;

echo "TEST2 : Value of result is $result<br/>";
?>

| Also Read | PHP Data Types

So this is for today, in our next tutorial we will learn about Control Statements(Decison Making).Stay connected with us for more tutorials,  keep visiting for more coming in this series . Feel free to like, comment ,share and give your opinions in comments below. Your valuable comments help us in giving you more relevant content . Be a part of Tekraze family, have a nice day.

RAHUL CHANANA

An undergraduate student from Guru Jambheshwar University currently doing B.tech in Information Technology, having two year of experience in Graphics Designing and in Web Development, loves to code. In free time i love to play cricket ,watch tv series and love to travell.