PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Operators

An operator is a character that represents an action.

Examples:

  • + is an arithmetic operator which represents addition.
  • - is an arithmetic operator which represents subtraction.

PHP has 11 types of operators.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Incrementing/Decrementing Operators
  6. String Operators
  7. Array Operators
  8. Type Operators
  9. Bitwise Operators
  10. Error Control Operators
  11. Execution Operators

Also, we have a new useful operator added in PHP 7.

Note: In this tutorial, we will only learn about the first 7 types of operators, as they are the ones that used most commonly.

PHP Arithmetic Operators

This is almost the same as the basic arithmetic operators you learned in your school.

OperatorNameExampleResult
+Addition$a + $bSum of $a and $bRun Example ››
-Subtraction$a - $bDifference of $a and $bRun Example ››
*Multiplication$a * $bProduct of $a and $bRun Example ››
/Division$a / $bQuotient of $a and $bRun Example ››
%Modulus$a % $bRemainder of $a / $bRun Example ››
**Exponentiation$a ** $bResult of raising $a to the $b'th powerRun Example ››

PHP Assignments Operators

The basic assignment operator in PHP is =. It is actually not the "equal" mark in PHP. It works as assignment operator, which assigns a value to a variable.

PHP Basic Assignment Operator Example


<?php
$a = 5;

$b = 7;
$a = $b;

echo $a; // outputs 7

Run Example ››

In this example,

  • 5 is assigned to $a.
  • 7 is assigned to $b.
  • Then, the value of $b (7) is assigned to $a.

There are some other assignment operators to learn.

OperatorExampleLong FormDescription
=$a = $b$a = $b$a gets set to the value of $bRun Example ››
+=$a += $b$a = $a + $b$a gets set to $a + $b (Addition)Run Example ››
-=$a -= $b$a = $a - $b$a gets set to $a - $b (Subtraction)Run Example ››
*=$a *= $b$a = $a * $b$a gets set to $a * $b (Multiplication)Run Example ››
/=$a /= $b$a = $a / $b$a gets set to $a / $b (Division)Run Example ››
%=$a %= $b$a = $a % $b$a gets set to $a % $b (Modulus)Run Example ››
**=$a **= $b$a = $a ** $b$a gets set to $a ** $b (Exponentiation)Run Example ››

PHP Comparison Operators

Comparison Operators allow you to compare two values.

OperatorNameExampleResult
==Equal$a == $bTrue if values of $a and $b are equalRun Example ››
===Identical$a === $bTrue if both values and data types of $a and $b are equalRun Example ››
!=Not equal$a != $bTrue if $a is not equal to $bRun Example ››
<>Not equal$a <> $bTrue if $a is not equal to $bRun Example ››
!==Not identical$a !== $bTrue if $a is not equal to $b or they are not in the same data typeRun Example ››
>Greater than$a > $bTrue if $a is greater than $bRun Example ››
<Less than$a < $bTrue if $a is less than $bRun Example ››
>=Greater than or equal to$a >= $bTrue if $a is greater than or equal to $bRun Example ››
<=Less than or equal to$a <= $bTrue if $a is less than or equal to $bRun Example ››
!== and <> have no difference. Both work in the same way.

PHP Logical Operators

A logical operator is a kind used in logic. In PHP, all logical operators return a boolean value.

OperatorNameExampleResult
andand$a and $bTrue if both $a and $b are trueRun Example ››
&&and$a && $bTrue if both $a and $b are trueRun Example ››
oror$a or $bTrue if either $a or $b is trueRun Example ››
||or$a || $bTrue if either $a or $b is trueRun Example ››
xorxor$a xor $bTrue if either $a or $b is true, but not bothRun Example ››
!not!$aTrue if $a is not trueRun Example ››

Note: We will discuss more about the if blocks we have used in this examples in the next lesson.

PHP Incrementing/Decrementing Operators

PHP's incrementing and decrementing operators are used to increment and decrement values respectively.

Note:

ExampleNameResult
++$aPre-incrementIncrements $a by one, then returns $aRun Example ››
$a++Post-incrementReturns $a, then increments $a by one.Run Example ››
--$aPre-decrementDecrements $a by one, then returns $aRun Example ››
$a--Post-decrementReturns $a, then decrements $a by one.Run Example ››

PHP String Operators

There are two special string operators what help us to operate strings.

OperatorNameExampleResult
.Concatenation$a . $bReturns the concatenation of its right and left argumentsRun Example ››
.=Concatenation Assignment$a .= $b$a gets set to $a . $bRun Example ››

PHP Array Operators

PHP array operators are used to perform operations on arrays.

OperatorNameExampleResult
+Union$a + $bReturns the union of $a and $b arrays.Run Example ››
+=Union Assignment$a += $b$a gets set to $a + $bRun Example ››
==Equal$a == $bTrue if $a and $b has equal key/value pairsRun Example ››
===Identical$a === $bTrue if $a and $b has equal key/value pairs in the same order and of same data type.Run Example ››
!=Not equal$a != $bTrue if $a and $b are not equal.Run Example ››
<>Not equal$a <> $bTrue if $a and $b are not equal.Run Example ››
!==Not identical$a !== $bTrue if $a and $b has different orders, different key/value pairs or values of different data types.Run Example ››

Spaceship Operator

Spaceship Operator (<=>) is a combined comparison operator which was added in PHP 7. Integers, floats, and strings can be compared.

WhenReturn Value
Either side are equal0
The left side is greater1
The right side is greater-1

PHP Spaceship Operator - Integer Comparison


<?php
var_dump(1 <=> 1); // outputs 0
var_dump(1 <=> 2); // outputs -1
var_dump(2 <=> 1); // outputs 1

Run Example ››

PHP Spaceship Operator - Float Comparison


<?php
var_dump(1.5 <=> 1.5); // outputs 0
var_dump(1.5 <=> 2.5); // outputs -1
var_dump(2.5 <=> 1.5); // outputs 1

Run Example ››

PHP Spaceship Operator - String Comparison


<?php
var_dump('a' <=> 'a'); // outputs 0
var_dump('a' <=> 'b'); // outputs -1
var_dump('b' <=> 'a'); // outputs 1

var_dump('eat' <=> 'ear'); // outputs 1

Run Example ››

PHP Spaceship Operator - Mixed Comparison


<?php
var_dump(1 <=> 2.5); // outputs -1
var_dump(2.5 <=> 1); // outputs 1

var_dump('1' <=> 1); // outputs 0
var_dump(1 <=> '2'); // outputs -1
var_dump('2' <=> 1); // outputs 1

Run Example ››
Comparing strings like "dog" with an integer like 10 can return unexpected results.
Facebook Twitter