PHP Operators Types with 10 example explained

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

Also read introduction to PHP language

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. A PHP operator is a symbol that is used to perform operations on variables or values. PHP operators can be classified into 8 types:

Also checkout Best PHP Scripts from Urbanscripts

PHP language supports following type of operators.

PHP Operators Types:

  • 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.
List Of Arithmetic PHP Operators Types

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
Comparsion PHP Operators types explained

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
?>

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
Logical PHP operators types explained

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
Assisgnment PHP operators types explained

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
Incrementing and Decrementing PHP Operators Types explained

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/>";
?>

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.

Content Protection by DMCA.com
RAHUL CHANANA
RAHUL CHANANA

UX /UI designer who loves clean, simple & unique design. i also enjoy crafting brand identities, icons, & ilustration work.

An Graduate Student of Information Technology, Guru Jambheshwar University of Science & Technology, Hisar. Love to discover new things about tech and loves to design.
Expertise in Graphic Designing with photoshop CS6 and CC. Also a little bit command over GIMP and CorelDRAW. I also have a knowledge of C, C++, Core Java, HTML5, CSS3, Bootstrap4, JavaScript and PHP. In free time i love to play cricket ,watch web series and love to travel.

Articles: 24

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *