MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Complete Guide and Tutorials for PHP Operators with example

What is Operators in PHP?

Operators are symbols which tell the PHP processor that certain actions are performed. The Add (+) symbol, for example, is an operator who tells PHP to add two variables or values, while the larger (>) symbol is an operator who tells PHP to match two values. Do not presume that operators are functions alike. Such operators may be used instead of functioning (such as the PHP ternary).

PHP Operators infographic listing nine types: arithmetic, string, assignment, logical, comparison, spaceship and more
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Spaceship operators

Arithmetic operators:-

The arithmetic operator is used for basic arithmetical operations like adding, removing, multiplying, etc. These operators can be used to execute simple arithmetic behaviour of numerical values. The following table shows how the results are used and what results are obtained.

Table of PHP arithmetic operators: add, subtract, multiply, divide, modulus and exponentiate with examples
  • Operator Name Example Result
  • + Addition $x + $y Sum of $x and $y
  • - Subtraction $x - $y Difference of $x and $y
  • * Multiplication $x * $y Product of $x and $y
  • / Division $x / $y Quotient of $x and $y
  • % Modulus $x % $y Remainder of $x divided by $y
  • ** Exponentiation $x ** $y Result of raising $x to the $y'th power (Introduced in PHP 5.6)

Example:–

Output:-

Output of the PHP arithmetic operators example showing results 10, 6, 16, 4, 0 and 64

Assignment operators:-

To assign values to variables, PHP Assignment operators are used. The left side of the operand variable is replaced with the right side value. Variables are assigned by operators in this group. The most common assignment operator is =, which assigns the right side of the operand to the variable on the left.

Table of PHP assignment operators =, +=, -=, *=, /= and %= with equivalent expressions
  • Assignment Same as… Description
  • x = y x = y The left operand gets set to the value of the expression on the right
  • x += y x = x + y Addition
  • x -= y x = x - y Subtraction
  • x *= y x = x * y Multiplication
  • x /= y x = x / y Division
  • x %= y x = x % y Modulus

Example:-

Output:-

Output of the PHP assignment operators example showing 20, 40, 20, 400, 20 and 0

Comparison operators:-

The operator for a comparison indicates the relationship shared by a number or a series, by two or more values. These php operators are simply used to compare the numbers or strings of the two values. Here we will instruct you how to use php operators to compare them. This type of PHP operator is essential to search for any inconsistencies or correlations between values and variables. It functions as booleans, returning the True or false value. For example, if the two variables you compares are not equal in value, using PHP not equal to the operator would show real.

Table of PHP comparison operators such as ==, ===, !=, <>, !==, >, <, >= and <= with results
  • Operator Name Example Result
  • == Equal $x == $y Returns true if $x is equal to $y
  • === Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
  • != Not equal $x != $y Returns true if $x is not equal to $y
  • <> Not equal $x <> $y Returns true if $x is not equal to $y
  • !== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
  • > Greater than $x > $y Returns true if $x is greater than $y
  • < Less than $x < $y Returns true if $x is less than $y
  • >= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
  • <= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y

Example:-

Outputs:-

Output of the PHP comparison operators example showing a list of boolean true and false results

Increment/Decrement operators:-

These operators increase the value of a vector (increase) or decrease it (decrease). Often they are referred to as uniform operators since only one operator works. Operators for increasing and decreasing are employed to constantly increase or decrease a value by a factor of 1. Operator for pre-increment When added to the value, the new $x value is returned. For a clearer idea see the table below:

Table of PHP increment and decrement operators: pre-increment, post-increment, pre-decrement, post-decrement
  • Operator Name Description
  • ++$x Pre-increment Increments $x by one, then returns $x
  • $x++ Post-increment Returns $x, then increments $x by one
  • --$x Pre-decrement Decrements $x by one, then returns $x
  • $x-- Post-decrement Returns $x, then decrements $x by one

Example:-

Output:-

Output of the PHP increment and decrement example showing values 26, 2625, 2624, 2425 and 24

Logical operators:-

They mix conditionals and are used mostly to track various conditions simultaneously. The PHP (or) operator, for example, tests that at least one in two is valid. You can use the PHP (and) operator if you want to verify if they are both valid. See the following table for clarifications:

Table of PHP logical operators and, or, xor, &&, || and ! with examples and results
  • Operator Name Example Result
  • and And $x and $y True if both $x and $y are true
  • or Or $x or $y True if either $x or $y is true
  • xor Xor $x xor $y True if either $x or $y is true, but not both
  • && And $x && $y True if both $x and $y are true
  • || Or $x || $y True if either $x or $y is true
  • ! Not !$x True if $x is not true

Example:-

Output:-

Output of the PHP logical operators example printing True six times

String operators:-

Two string operators are available. The use of numerical values could produce unpredictable results:

  1. The operator of PHP (.) is also known as the operator of concatenation. It connects individual strings.
  2. The operator of PHP (.=) is known as the operator of the combined assignment. It attaches the argument to the one on the left on the right page.
Table of PHP string operators: dot for concatenation and .= for concatenation assignment
  • Operator Name Example Result
  • . Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
  • .= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1

Example:-

Output:-

PHP string operator example output printing BitDegree twice

Array operators:-

For comparing arrays, the array operators are used. Use the operators mentioned below to compare different arrays. Be sure the right grammar is used.

PHP array operators table: union, equality, identity, inequality and non-identity with examples
  • Operator Name Example Result
  • + Union $x + $y Union of $x and $y
  • == Equality $x == $y Returns true if $x and $y have the same key/value pairs
  • === Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types
  • != Inequality $x != $y Returns true if $x is not equal to $y
  • <> Inequality $x <> $y Returns true if $x is not equal to $y
  • !== Non-identity $x !== $y Returns true if $x is not identical to $y

Example:-

Output:-

PHP array operator example output: bool(false), bool(false), bool(true), bool(true), bool(true)

Spaceship operators:-

PHP 7 adds a new spacecraft operator (<=>) that can be used to compare two terms. The combined contrast operator is also known.

If the two operands are identical, the spacecraft operator returns 0, 1 if the left is larger, and -1 if the right is larger. The three-way comparison is essentially shown as seen in the next table:

Table mapping PHP comparison operators to equivalent spaceship operator <=> results of -1, 0 and 1

Example:-

Output:-

PHP spaceship operator example output showing 0, -1 and 1 for three comparison sets

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
👤 About the Author <strong>Ashwani</strong> is passionate about DevOps, DevSecOps, SRE, MLOps, and AiOps, with a strong drive to simplify and scale modern IT operations. Through continuous learning and sharing, Ashwani helps organizations and engineers adopt best practices for automation, security, reliability, and AI-driven operations. 🌐 Connect & Follow: <strong>Website:</strong> <a href="https://www.wizbrand.com/">WizBrand.com</a> <strong>Facebook:</strong> <a href="https://www.facebook.com/DevOpsSchool">facebook.com/DevOpsSchool</a> <strong>X (Twitter):</strong> <a href="https://x.com/DevOpsSchools">x.com/DevOpsSchools</a> <strong>LinkedIn:</strong> <a href="https://www.linkedin.com/company/devopsschool">linkedin.com/company/devopsschool</a> <strong>YouTube:</strong> <a href="https://www.youtube.com/@TheDevOpsSchool">youtube.com/@TheDevOpsSchool</a> <strong>Instagram:</strong> <a href="https://www.instagram.com/devopsschool/">instagram.com/devopsschool</a> <strong>Quora:</strong> <a href="https://devopsschool.quora.com/">devopsschool.quora.com</a> <strong>Email</strong>- contact@devopsschool.com

Related Posts

PHP Code for Godaddy VPS Server SMTP Email Relay

This page shows how to send email from a GoDaddy VPS through GoDaddy's SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below…

Read More

Laravel: oauth_access_tokens clean up process

using Laravel Passport—the oauth_access_tokens table balloons when tokens aren’t expiring, not being revoked on logout, or not being purged. Here’s a practical, safe cleanup + prevention plan…

Read More

Laravel: Complete Guide of Laravel Passport

Here’s a comprehensive, up-to-date step-by-step tutorial for setting up and using Laravel Passport (OAuth2 API authentication) with clear explanations, working code samples, and troubleshooting tips—suitable for local…

Read More

Laravel: What is APP_KEY in Laravel?

Let’s break down both what APP_KEY is in a Laravel .env file, why it matters, and its use cases. I’ll also explain how it’s generated and what…

Read More

XAMPP – LAMPP Tutorials: List of Production Files to Cleanup Guide

Here’s a comprehensive and updated tutorial on which files should be cleaned up regularly in a production environment running XAMPP (under /opt/lampp), what each file/folder is, and…

Read More

artisan Tutorials: php artisan passport

When you run php artisan passport:install, Laravel attempts to do the following: Running php artisan passport:install is a crucial setup step when using Laravel Passport for API…

Read More