0% found this document useful (0 votes)
3 views17 pages

PHP Operators

The document provides an overview of PHP operators, which are special symbols used to perform operations on variables and values. It categorizes operators into types such as arithmetic, logical, comparison, conditional, assignment, array, increment/decrement, and string operators, each with their syntax and examples. Understanding these operators is essential for writing effective PHP code.

Uploaded by

Kusuma gaanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

PHP Operators

The document provides an overview of PHP operators, which are special symbols used to perform operations on variables and values. It categorizes operators into types such as arithmetic, logical, comparison, conditional, assignment, array, increment/decrement, and string operators, each with their syntax and examples. Understanding these operators is essential for writing effective PHP code.

Uploaded by

Kusuma gaanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Search...

PHP Tutorial PHP Exercises PHP Array PHP String PHP Calendar PHP Filesystem Sign In

PHP Operators
Last Updated : 05 Apr, 2025

In PHP, operators are special symbols used to perform operations on


variables and values. Operators help you perform a variety of tasks, such
as mathematical calculations, string manipulations, logical comparisons,
and more. Understanding operators is essential for writing effective and
efficient PHP code. PHP operators are categorized into several types:

Let us now learn about each of these operators in detail.

1. Arithmetic Operators
Arithmetic operators are used to perform basic arithmetic operations like
addition, subtraction, multiplication, division, and modulus.

Operator Name Syntax Operation

+ Addition $x + $y Sum the operands

- Subtraction $x - $y Differences in the Operands

* Multiplication $x * $y Product of the operands

/ Division $x / $y The quotient of the operands

** Exponentiation $x ** $y $x raised to the power $y

We use cookies
%
to ensure you have the best browsing
Modulus
experience on our website. By
$x % $y The remainder of the operands
using our site, you acknowledge that you have read and understood our Cookie Policy & Got It !
Privacy Policy
Note: The exponentiation has been introduced in PHP 5.6.

Example: This example explains the arithmetic operators in PHP.

<?php
// Define two numbers
$x = 10;
$y = 3;

// Addition
echo "Addition: " . ($x + $y) . "\n";

// Subtraction
echo "Subtraction: " . ($x - $y) . "\n";

// Multiplication
echo "Multiplication: " . ($x * $y) . "\n";

// Division
echo "Division: " . ($x / $y) . "\n";

// Exponentiation
echo "Exponentiation: " . ($x ** $y) . "\n";

// Modulus
echo "Modulus: " . ($x % $y) . "\n";
?>

Output

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333
Exponentiation: 1000
Modulus: 1
We use cookies to ensure you have the best browsing experience on our website. By
using 2. Logical
our site, Operators
you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
Logical operators are used to operate with conditional statements. These
operators evaluate conditions and return a boolean result (true or false).

Operator Name Syntax Operation

Logical $x and True if both the operands are true else


and
AND $y false

Logical True if either of the operands is true


or $x or $y
OR otherwise, it is false

Logical $x xor True if either of the operands is true and


xor
XOR $y false if both are true

Logical $x && True if both the operands are true else


&&
AND $y false

Logical True if either of the operands is true


|| $x || $y
OR otherwise, it is false

Logical
! !$x True if $x is false
NOT

Example: This example describes the logical & relational operators in PHP.

<?php
$x = 50;
$y = 30;
if ($x == 50 and $y == 30)
echo "and Success \n";

if ($x == 50 or $y == 20)
echo "or Success \n";
We use cookies to ensure you have the best browsing experience on our website. By
if ($x == 50 xor $y == 20)
using our site, you acknowledge that you have read and understood our Cookie Policy &
echo "xor Success \n";
Privacy Policy
if ($x == 50 && $y == 30)
echo "&& Success \n";

if ($x == 50 || $y == 20)
echo "|| Success \n";

if (!$z)
echo "! Success \n";
?>

Output

and Success
or Success
xor Success
&& Success
|| Success
! Success

3. Comparison Operators
Comparison operators are used to compare two values and return a
Boolean result (true or false).

Operator Name Syntax Operation

Returns True if both the operands


== Equal To $x == $y
are equal

Returns True if both the operands


!= Not Equal To $x != $y
are not equal

Returns True if both the operands


<> Not Equal To $x <> $y
areByunequal
We use cookies to ensure you have the best browsing experience on our website.
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
Operator Name Syntax Operation

$x === Returns True if both the operands


=== Identical
$y are equal and are of the same type

Returns True if both the operands


!== Not Identical $x == $y are unequal and are of different
types

< Less Than $x < $y Returns True if $x is less than $y

> Greater Than $x > $y Returns True if $x is greater than $y

Less Than or Returns True if $x is less than or


<= $x <= $y
Equal To equal to $y

Greater Than Returns True if $x is greater than or


>= $x >= $y
or Equal To equal to $y

Example: This example describes the comparison operator in PHP.

<?php
$a = 80;
$b = 50;
$c = "80";

// Here var_dump function has been used to


// display structured information. We will learn
// about this function in complete details in further
// articles.
var_dump($a == $c) + "\n";
var_dump($a != $b) + "\n";
var_dump($a <> $b) + "\n";
var_dump($a === $c) + "\n";
var_dump($a !== $c) + "\n";
We use cookies to ensure you have the best browsing experience on our website. By
var_dump($a
using our site, you acknowledge<that$b) + "\n";
you have read and understood our Cookie Policy &
var_dump($a > $b) + "\n";
Privacy Policy
var_dump($a <= $b) + "\n";
var_dump($a >= $b);
?>

Output

bool(true)

Warning: A non-numeric value encountered in


/home/guest/sandbox/Solution.php on line 10
bool(true)

Warning: A non-numeric value encountered in


/home/guest/sandbox/Solution.php on line 11
...

4. Conditional or Ternary Operators


These operators are used to compare two values and take either of the
results simultaneously, depending on whether the outcome is TRUE or
FALSE. These are also used as a shorthand notation for the if...else
statement that we will read in the article on decision making.

Syntax:

$var = (condition)? value1 : value2;

Here, the condition will either be evaluated as true or false. If the condition
evaluates to True, then value1 will be assigned to the variable $var;
otherwise, value2 will be assigned to it.

Operator Name Operation

If the condition is true? then $x : or else $y. This means


?: Ternary that if the condition is true, then the left result of the
We use cookies to ensure you havecolon
the bestisbrowsing
accepted otherwise,
experience the result
on our website. By is on the right.
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
Example: This example describes the Conditional or Ternary operators in
PHP.

<?php
$x = -12;
echo ($x > 0) ? 'The number is positive' : 'The number is
negative';
?>

Output

The number is negative

5. Assignment Operators
Assignment operators are used to assign values to variables. These
operators allow you to assign a value and perform operations in a single
step.

Operator Name Syntax Operation

Operand on the left obtains the


= Assign $x = $y
value of the operand on the right

$x += Simple Addition same as $x = $x


+= Add then Assign
$y + $y

Subtract then $x -= Simple subtraction same as $x =


-=
Assign $y $x - $y

Multiply then $x *= Simple product same as $x = $x *


*=
Assign $y $y

We use cookies to ensure you have


Divide, thentheassign
best browsing
$xexperience on our website.
/= Simple By same as $x = $x /
division
/=
using our site, you acknowledge that you have read and
(quotient) $y understood$y
our Cookie Policy &
Privacy Policy
Operator Name Syntax Operation

Divide, then assign $x %= Simple division same as $x = $x


%=
(remainder) $y % $y

Example: This example describes the assignment operator in PHP.

<?php
// Simple assign operator
$y = 75;
echo $y, "\n";

// Add then assign operator


$y = 100;
$y += 200;
echo $y, "\n";

// Subtract then assign operator


$y = 70;
$y -= 10;
echo $y, "\n";

// Multiply then assign operator


$y = 30;
$y *= 20;
echo $y, "\n";

// Divide then assign(quotient) operator


$y = 100;
$y /= 5;
echo $y, "\n";

// Divide then assign(remainder) operator


$y = 50;
$y %= 5;
echo $y;
We use?>cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
Output

75
300
60
600
20
0

6. Array Operators
These operators are used in the case of arrays. Here are the array
operators, along with their syntax and operations, that PHP provides for
the array operation.

Operator Name Syntax Operation

+ Union $x + $y Union of both, i.e., $x and $y

$x == Returns true if both have the same key-


== Equality
$y value pair

!= Inequality $x != $y Returns True if both are unequal

Returns True if both have the same key-


$x ===
=== Identity value pair in the same order and of the
$y
same type

Non- $x !== Returns True if both are not identical to


!==
Identity $y each other

$x <>
<> Inequality
We use cookies to ensure you have the best browsing Returns
experienceTrue
on ourifwebsite.
both areBy unequal
$y
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
Example: This example describes the array operation in PHP.

<?php
$x = array("k" => "Car", "l" => "Bike");
$y = array("a" => "Train", "b" => "Plane");

var_dump($x + $y);
var_dump($x == $y) + "\n";
var_dump($x != $y) + "\n";
var_dump($x <> $y) + "\n";
var_dump($x === $y) + "\n";
var_dump($x !== $y) + "\n";
?>

Output:

array(4) {
["k"]=>
string(3) "Car"
["l"]=>
string(4) "Bike"
["a"]=>
string(5) "Train"
["b"]=>
string(5) "Plane"
}
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)

7. Increment/Decrement Operators
These are called the unary operators as they work on single operands.
These are used to increment or decrement values.

We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
Operator Name Syntax Operation

First, increment $x by one, then return


++ Pre-Increment ++$x
$x

First, decrement $x by one, then return


-- Pre-Decrement --$x
$x

First returns $x, then increment it by


++ Post-Increment $x++
one

Post- First, return $x, then decrement it by


-- $x--
Decrement one

Example: This example describes the Increment/Decrement operators in


PHP.

<?php
$x = 2;
echo ++$x, " First increments then prints \n";
echo $x, "\n";

$x = 2;
echo $x++, " First prints then increments \n";
echo $x, "\n";

$x = 2;
echo --$x, " First decrements then prints \n";
echo $x, "\n";

$x = 2;
echo $x--, " First prints then decrements \n";
echo $x;
?>

We use cookies to ensure you have the best browsing experience on our website. By
using Output
our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
3 First increments then prints
3
2 First prints then increments
3
1 First decrements then prints
1
2 First prints then decrements
1

8. String Operators
This operator is used for the concatenation of 2 or more strings using the
concatenation operator ('.'). We can also use the concatenating assignment
operator ('.=') to append the argument on the right side to the argument on
the left side.

Operator Name Syntax Operation

. Concatenation $x.$y Concatenated $x and $y

Concatenation and First, it concatenates then


.= $x.=$y
assignment assigns, the same as $x = $x.$y

Example: This example describes the string operator in PHP.

<?php
$x = "Geeks";
$y = "for";
$z = "Geeks!!!";
echo $x . $y . $z, "\n";
$x .= $y . $z;
echo $x;
?>

Output
We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
GeeksforGeeks!!!
GeeksforGeeks!!!

Comment More info


Next Article
Campus Training Program PHP | Bitwise Operators

Similar Reads
PHP 7 | Spaceship Operator
This article will make you aware of a very useful operator i.e the spaceship
operator PHP 7. The spaceship operator or combined comparison operator i…

8 min read

PHP Loops
In PHP, Loops are used to repeat a block of code multiple times based on a
given condition. PHP provides several types of loops to handle different…

15+ min read

PHP | fopen( )
The fopen() function in PHP is used to open a file or URL. It returns a file
pointer that can be used with other functions like fread(), fwrite(), and…

15+ min read

PHP Arrays
Arrays are one of the most important data structures in PHP. They allow you
to store multiple values in a single variable. PHP arrays can hold values of…

15+ mintoread
We use cookies ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
PHP Cookies
Cookies in PHP are used for maintaining state and storing user-specific
information across multiple page visits. Since HTTP is a stateless protocol,…

15+ min read

PHP | Unique Features


As PHP can do anything related to server-side programming which contains
the backend of any web page, it holds a lot of unique features within it. The…

15+ min read

PHP Array Functions


Arrays are one of the fundamental data structures in PHP. They are widely
used to store multiple values in a single variable and can store different…

15+ min read

PHP | Types of Errors


In PHP, errors are an essential part of the development process. They help
developers identify issues with their code and ensure that applications run…

15+ min read

PHP ereg() Function


The Ereg() function in PHP searches a string to match the regular expression
given in the pattern. The function is case-sensitive. This function has been…

9 min read

PHP eval() Function


PHP eval() function in PHP is an inbuilt function that evaluates a string as
PHP code. Syntax: eval( $string ) Parameters: This function accepts a single…

7 min read
We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)

Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305

Advertise with us

Company Explore
About Us Job-A-Thon
Legal Offline Classroom Program
Privacy Policy DSA in JAVA/C++
Careers Master System Design
In Media Master CP
Contact Us Videos
Corporate Solution
Campus Training Program

Tutorials DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android

Data Science
We use cookies to ensure you have&the
MLbest browsing experience on our website.Web
By Technologies
using our site, you acknowledge thatPython
Data Science With you have read and understood our Cookie Policy & HTML
Privacy Policy
Machine Learning CSS
ML Maths JavaScript
Data Visualisation TypeScript
Pandas ReactJS
NumPy NextJS
NLP NodeJs
Deep Learning Bootstrap
Tailwind CSS

Python Tutorial Computer Science


Python Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps System Design


Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
DevOps Roadmap System Design Bootcamp
Interview Questions

School Subjects Databases


Mathematics SQL
Physics MYSQL
Chemistry PostgreSQL
Biology PL/SQL
Social Science MongoDB
English Grammar

Preparation Corner More Tutorials


Company-Wise Recruitment Process Software Development
Aptitude Preparation Software Testing
Puzzles Product Management
Company-Wise Preparation Project Management
Linux
Excel
All Cheat Sheets
We use cookies to ensure you have the best browsing experience on our website. By
Courses
using our site, you acknowledge that you have read and understood our CookieProgramming
Policy & Languages
IBM Certification Courses
Privacy Policy C Programming with Data Structures
DSA and Placements C++ Programming Course
Web Development Java Programming Course
Data Science Python Full Course
Programming Languages
DevOps & Cloud

Clouds/Devops GATE 2026


DevOps Engineering GATE CS Rank Booster
AWS Solutions Architect Certification GATE DA Rank Booster
Salesforce Certified Administrator Course GATE CS & IT Course - 2026
GATE DA Course 2026
GATE Rank Predictor

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy &
Privacy Policy

You might also like