Preloader

PHP Operators

PHP Tutorials
codevigyaan php

PHP automatically knows the type based on the value supplied.They allow you to do calculations compare values and make decisions in your code. Operators are a core part of writing dynamic and logical PHP programs.

What Are Operators in PHP

An operator is a symbol that tells PHP to perform a specific action. The action can be mathematical comparison based logical or conditional depending on the operator used.

Example

<?php
$m = 40;
$n = 58;
$sum = $m+ $n;
?>

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division etc.

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
%Modulus remainder

Example

<?php
$x = 20;
$y = 3;

echo $x + $y;
echo $x - $y;
echo $x * $y;
echo $x / $y;
echo $x % $y;
?>

Assignment Operators

Assignment operators assign values to variables and can also perform operations during assignment.

OperatorExampleMeaning
=$a = 5Assign value
+=$a += 2$a = $a + 2
-=$a -= 2$a = $a – 2
*=$a *= 2$a = $a * 2
/=$a /= 2$a = $a / 2

Example

<?php
$count = 10;
$count += 5;
?>

Comparison Operators

Comparison operators compare two values and return output in true or false. They are mostly used in making conditions.

OperatorDescription
==Equal
===Identical value and type
!=Not equal
!==Not identical
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Example

<?php
$a = 10;
$b = "10";

var_dump($a == $b);
var_dump($a === $b);
?>

Logical Operators

Logical operators are used to combine multiple conditions together.

OperatorDescription
&&And
|| Or
!Not

Example

<?php
$age = 22;
$isStudent = true;

if ($age > 18 && $isStudent) {
    echo "Access granted";
}
?>

Increment and Decrement Operators

These operators increase or decrease a variable’s value by one.

OperatorDescription
++Increment
Decrement

Example

<?php
$i = 5;
$i++;
$i--;
?>

String Operators

PHP provides a string operator to combine text values.

OperatorDescription
.Concatenation

Example

<?php
$firstName = "Varsha";
$lastName = "Sharma";

echo $firstName . " " . $lastName;
?>

Ternary Operator

The ternary operator is a short way to write simple if else conditions. It is useful when you need to assign a value based on a condition.

Syntax

condition ? value_if_true : value_if_false;

Example

<?php
$age = 17;
$result = ($age >= 18) ? "Adult" : "Minor";

echo $result;
?>

The code checks the condition and returns one of the two values based on the result.

Operator Precedence

When there are several operators in one expression, PHP evaluates them according to a certain precedence. This can be controlled by using parentheses.

Example

<?php
$result = (10 + 5) * 2;
?>

The Importance of PHP Operators

PHP operators are critical in developing application logic. They aid in calculations, comparisons, decision-making, and the processing of user input. PHP operators are used in almost every PHP application.

Link to PHP Introduction
Link to PHP Syntax and Comments
Link to PHP Variables and Data Types

You may also like...

1 Response

  1. December 23, 2025

    […] to PHP Control StructuresLink to PHP OperatorsLink to PHP Variables and Data […]

Leave a Reply

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