Skip to content
  • Quizes
  • QStack
  • Blog
  • pinterest
  • instagram
  • twitter
  • linkedin
  • facebook
W3 Coding Schools
  • Home
  • HTML
    • HTML Introduction
    • HTML Editors
    • Fundamentals of HTML
    • Elements of HTML
    • HTML Attributes
    • HTML Headings
    • HTML Paragraphs
    • HTML Style Attribute
    • HTML Text Formatting
    • HTML Phrase Tags
    • HTML Comments
    • HTML with CSS
    • HTML Links
    • HTML Images
    • HTML Tables
    • HTML Lists
    • HTML Blocks
    • HTML Class Attribute
    • HTML Id Attribute
    • HTML Iframes
    • HTML JavaScript
    • HTML File Paths
    • HTML Head
    • Computer Code Elements
    • HTML Entities
    • HTML Charset
    • HTML URL Encode
    • HTML and XHTML
    • HTML Layouts
    • HTML Forms
    • HTML Form Elements
    • HTML Form Input Types
    • HTML Input Attributes
    • HTML5 Introduction
    • HTML5 New Elements
    • Semantic Elements
    • HTML5 Migration
    • Style Guide
    • HTML Canvas
  • CSS
    • CSS Introduction
    • CSS Syntax and CSS Comments
    • CSS Selectors
    • How to add CSS to a Webpage
    • CSS Color Basics
    • CSS Background Properties
    • CSS Border Properties
    • CSS Margin and Padding Properties
    • CSS Height and Width properties
    • CSS Box Model
    • CSS Outline Properties
    • CSS Fonts
    • CSS Text
    • How To Add Icons
    • CSS Links
    • CSS List-Style
    • CSS Tables
    • CSS Display Property
    • CSS Position Property
    • CSS Overflow Property
    • CSS max-width Property
    • CSS Float and Clear Properties
    • CSS Alignment
    • CSS inline-block
    • CSS Combinators
    • CSS Pseudo Classes
    • CSS Pseudo Elements
    • CSS Opacity
    • CSS Navigation Bar
    • CSS Dropdowns
    • CSS Image Gallery
    • CSS Image Sprites
    • CSS Attribute Selector
    • CSS Styling Forms
    • CSS Counters
    • CSS Units
    • CSS Specificity
    • CSS Website Layout
    • CSS Rounded Corners
    • CSS Border Image Property
    • CSS Multiple Backgrounds
    • CSS Gradient
    • CSS Shadow Effects
    • CSS Text Effects
    • CSS Web Fonts
    • CSS 2D Transforms
  • Bootstrap 4
    • Bootstrap 4 – Introduction
    • Bootstrap versions
    • Bootstrap 4 Layout
    • Bootstrap 3 Vs Bootstrap 4
    • Bootstrap 4 Grid System
    • Bootstrap 4 Typography
    • Bootstrap 4 Colors
    • Bootstrap 4 Images
    • Bootstrap 4 Tables
    • Bootstrap 4 Jumbotron
    • Bootstrap 4 Figures
    • Bootstrap 4 Alerts
    • Bootstrap 4 Buttons
    • Bootstrap 4 Button Group
    • Bootstrap 4 Badges
    • Bootstrap 4 Spinners
    • Bootstrap 4 Progress Bars
    • Bootstrap 4 Pagination
    • Bootstrap 4 Breadcrumbs
    • Bootstrap 4 List Groups
    • Bootstrap 4 Cards
  • jQuery
    • jQuery Introduction
    • jQuery Download
    • jQuery Selectors
    • jQuery Event Methods
    • jQuery Hide/Show Effects
    • jQuery Fading Effects
    • jQuery Sliding Effects
    • jQuery Animation
    • jQuery Stop and Callback
    • jQuery Get and Set Methods
    • jQuery Chaining
    • jQuery Add
    • jQuery Remove
    • jQuery CSS Classes
    • jQuery Style Properties
    • jQuery Dimensions
    • jQuery Traversing
    • jQuery Ancestors
    • jQuery Descendants
    • jQuery Siblings
  • Javascript
    • JS Introduction
    • JS Where to Put
    • JavaScript Syntax
    • JavaScript Comments
    • JavaScript Variables
    • JavaScript Data Types
    • JavaScript Operators
    • JavaScript Events
    • JavaScript Strings
    • JavaScript Numbers
  • php
    • PHP Introduction
    • Install PHP
    • PHP Syntax and Comments
    • PHP Variables
    • PHP Constants
    • PHP Echo and Print
    • PHP Data Types
    • PHP Strings
    • PHP Operators
    • PHP $ and $$ Variables
  • WordPress
    • WordPress Introduction
    • WordPress History
    • WordPress.com vs WordPress.org
    • How to Install WordPress
    • WordPress Dashboard
    • How to Create a WordPress Website
    • WordPress Posts
    • WordPress Pages
    • WordPress Posts vs Pages
    • WordPress Categories
  • SEO
    • SEO Introduction
    • SEO Tactics and Methods
    • SEO Relevant Filenames
    • SEO Domain Name
    • Website Design and SEO
    • SEO Keywords
    • Meta Tags Optimization
    • Title Tag Optimization
    • Anchor Text Optimization
    • Content Optimization
  • Android
    • Android Introduction
    • Android History and Versions
    • Android Architecture
    • Android Environment Setup
    • Android Application Components
    • Hello World Application
    • Android Activities
  • iOS
    • iOS Introduction
    • iOS Environment Setup
    • iOS Architecture

You Are Here

  • Home
  • Learn php
  • PHP Operators

Topics

  • PHP $ and $$ Variables
  • PHP Operators
  • PHP Strings
  • PHP Data Types
  • PHP Echo and Print
  • PHP Constants
  • PHP Variables
  • PHP Syntax and Comments
  • Install PHP
  • PHP Introduction

Oct Champs & Prizes

  • 1. Pooja Ladda
  • 2. Manjali Kuldharan
  • 3. Pranali Surawar
  • 4. Anjali Kulkarni
  • 5. Vishal Deshmukh

Recent Posts

  • HTML Media
  • jQuery Siblings
  • Bootstrap 4 Cards
  • jQuery Descendants
  • jQuery Ancestors

PHP Operators

PHP operator is a character or symbol that performs a special operation within the PHP code. For example, If you use the equals sign ( = ) to assign a value to a variable, you are using an assignment operator. If you add two numbers together using a plus sign ( + ), you are using an arithmetic operator.
The following are the lists of different operators used in PHP.
  • Arithmetic Operators
  • Assignment Operators
  • Logical or Relational Operators
  • Comparison Operators
  • Conditional or Ternary Operators
  • Increment/Decrement Operators
  • Array Operators
  • String Operators
  • Spaceship Operators

Arithmetic Operators in PHP:

The arithmetic operators are used to perform simple arithmetic operations such as addition, subtraction, multiplication, division, etc. The list of arithmetic operators are as follows:
Operator Name Example Description
+ Addition $a + $b Sum of $a and $b
- Subtraction $a - $b Difference of $a and $b
* Multiplication $a * $b Product of $a and $b
/ Division $a / $b Quotient of $a and $b
% Modulus $a % $b Remainder of $a divided by $b

Example:

<?php
$a = 15;
$b = 5;
echo($a + $b), "\n";  // 0utputs: 20
echo($a - $b), "\n";  // 0utputs: 10
echo($a * $b), "\n";  // 0utputs: 75
echo($a / $b), "\n";  // 0utputs: 3
echo($a % $b), "\n";  // 0utputs: 0
?>
Try it

Assignment Operators in PHP:

The assignment operators assign the values to a variable. The commonly used assignment operator is =, which assigns the variable on the left the value on the right side of the operand. The list of assignment operators are as follows:
Operator Name Example Same as
= Assign $a = $b $a = $b
+= Add and assign $a += $b $a = $a + $b
-= Subtract and assign $a -= $b $a = $a - $b
*= Multiply and assign $a *= $b $a = $a * $b
/= Divide and assign quotient $a /= $b $a = $a / $b
%= Divide and assign modulus $a %= $b $a = $a % $b

Example:

<?php
$a = 15;
echo $a, "\n";  //output: 15
$b = 3;
$b += 10;
echo $b, "\n";  //output: 13
$c = 25;
$c -= 10;
echo $c, "\n";  //output: 15
$d = 2;
$d *= 4;
echo $d, "\n";  //output: 8
$e = 100;
$e /= 10;
echo $e, "\n";  //output: 10
$f = 10;
$f %= 4;
echo $f;  //output: 2
?>
Try it

Comparison Operators in PHP:

The list of comparison operators are as follows:
Operator Name Example Result
== Equal $a == $b True if $a is equal to $b
=== Identical $a === $b True if $a is equal to $b, and they are of the same type
!= Not equal $a != $b True if $a is not equal to $b
<> Not equal $a <> $b True if $a is not equal to $b
!== Not identical $a !== $b True if $a is not equal to $b, or they are not of the same type
< Less than $a < $b True if $a is less than $b
> Greater than $a > $b True if $a is greater than $b
>= Greater than or equal to $a >= $b True if $a is greater than or equal to $b
<= Less than or equal to $a <= $b True if $a is less than or equal to $b

Example:

<?php
$a = 10;
$b = 25;
$c = "10";
var_dump($a == $c) + "\n";  //output: bool(true)
var_dump($a === $c) + "\n";  //output: bool(false)
var_dump($a != $b) + "\n";  //output: bool(true)
var_dump($a !== $c) + "\n";  //output: bool(true)
var_dump($a < $b) + "\n";  //output: bool(true)
var_dump($a > $b) + "\n";  //output: bool(false)
var_dump($a <= $b) + "\n";  //output: bool(true)
var_dump($a >= $b) + "\n";  //output: bool(false)
?>
Try it

Logical or Relational Operators in PHP:

The logical operators combine conditionals and are mostly used to check multiple conditions at once. For example, PHP “or” operator checks whether at least one value is true out of two values. If you need to check whether both of them are true, you should use PHP and the operator.
Operator Name Example Result
and And $a and $b True if both $a and $b are true
or Or $a or $b True if either $a or $b is true
xor Xor $a xor $b True if either $a or $b is true, but not both
&& And $a && $b True if both $a and $b are true
|| Or $a || $b True if either $a or $b is true
! Not !$a True if $a is not true
> Greater than $a > $b True if $a is greater than $b

Example:

<?php
$x = 10;
$y = 13;
$z = false;
if ($x == 10 and $y == 13)
echo "True \n";
if ($x == 12 or $y == 13)
echo "True \n";
if ($x == 10 xor $y == 9)
echo "True \n";
if ($x == 10 && $y == 13)
echo "True \n";
if ($x == 10 || $y == 14)
echo "True \n";
if (!($x == 19))
echo "True";
?>
Try it

Conditional or Ternary Operators in PHP:

The conditional operators are used to compare two values and take either of the results simultaneously, depending on whether the outcome is TRUE or FALSE.
Operator Name Example
? : Ternary If Condition is true ? Then value X : Otherwise value Y

Example:

<?php
$x = 10;
echo ($x > 0) ? 'The number is positive' : 'The number is negative';
?>
Try it

Incrementing and Decrementing Operators in PHP:

The increment or decrement operators increase (increment) or decrease (decrement) the value of a variable. They are also called as unary operators because they work on just one operand. The list of these operators are as follows:
Operator Name Effect
++$a Pre-increment Increments $a by one, then returns $a
$a++ Post-increment Returns $a, then increments $a by one
--$a Pre-decrement Decrements $a by one, then returns $a
$a-- Post-decrement Returns $a, then decrements $a by one

Example:

<?php
$a = 20;
echo ++$a, "\n";  // Outputs: 21
echo $a, "\n";  // Outputs: 21
$a = 20;
echo $a++, "\n";  // Outputs: 20
echo $a, "\n";  // Outputs: 21
$a = 20;
echo --$a, "\n";  // Outputs: 19
echo $a, "\n";  // Outputs: 19
$a = 20;
echo $a--, "\n";  // Outputs: 20
echo $a, "\n";  // Outputs: 19
?>
Try it

Array Operators in PHP:

These operators are used to compare arrays. The list of array operators are as follows:
Operator Name Example Result
+ Union $a + $b Union of $a and $b
== Equality $a == $b True if $a and $b have the same key/value pairs
=== Identity $a === $b True if $a and $b have the same key/value pairs in the same order and of the same types
!= Inequality $a != $b True if $a is not equal to $b
<> Inequality $a <> $b True if $a is not equal to $b
!== Non-identity $a !== $b True if $a is not identical to $b

Example:

<?php
$a = array("x" => "Apple", "y" => "Banana", "x" => "Mango");
$b = array("u" => "black", "v" => "white", "w" => "Yellow");
var_dump($a + $b);
var_dump($a == $b)+ "\n";
var_dump($a === $b)+ "\n";
var_dump($a != $b)+ "\n";
var_dump($a <> $b)+ "\n";
var_dump($a !== $b)+ "\n";
?>
Try it

String Operators in PHP:

The list of string operators are as follows:
Operator Name Example Result
. Concatenation $a . $b Concatenation of $a and $b
.= Concatenation assignment $a .= $b Appends the $b to the $a

Example:

<?php
$a = "W3coding";
$b = "Schools";
echo $a . $b, "\n";  // Outputs: W3codingSchools
$a .= $b;
echo $a;  // Outputs: W3codingSchools
?>
Try it

Spaceship Operators in PHP

PHP 7 introduced a new kind of operator called a spaceship operator. Similarly to some other kinds of operators, it compares two values (integers, floats, or strings). However, it returns an integer instead of a boolean value.
Operator Syntax Operation
$a < $b $a <=> $b Identical to -1 (right is greater)
$a > $b $a <=> $b Identical to 1 (left is greater)
$a <= $b $a <=> $b Identical to -1 (right is greater) or identical to 0 (if both are equal)
$a >= $b $a <=> $b Identical to 1 (if left is greater) or identical to 0 (if both are equal)
$a == $b $a <=> $b Identical to 0 (both are equal)
$a != $b $a <=> $b Not Identical to 0

Example:

<?php
echo(4 <=> 4);  //Output: 0
echo "\n";
echo(2 <=> 5);  //Output: -1
echo "\n";
echo(7 <=> 2);  //Output: 1
echo "\n";
?>
Try it

Post navigation

PHP Strings
PHP $ and $$ Variables

Ask a Question Cancel reply

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

W3 CODING SCHOOLS © Copyright 2019-20
Privacy policy   Terms of use

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok