Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

PHP Operators Arithmetic, Comparison, Logical: Tutorial, Examples, FAQs & Interview Tips

Operators are symbols that tell PHP to perform specific operations on values or variables. PHP supports a rich set of operators grouped by their purpose.

Arithmetic Operators

Used to perform basic math operations: + (addition), - (subtraction), * (multiplication), / (division), % (modulus), ** (exponentiation).

Arithmetic Operators
<?php
$a = 10;
$b = 3;

echo $a + $b;   // 13
echo $a - $b;   // 7
echo $a * $b;   // 30
echo $a / $b;   // 3.333...
echo $a % $b;   // 1  (remainder)
echo $a ** $b;  // 1000 (10 to the power of 3)
?>

Assignment Operators

Used to assign values to variables. Compound assignment operators combine an arithmetic operation with assignment.

Assignment Operators
<?php
$x = 10;   // assign 10
$x += 5;   // $x = $x + 5  -> 15
$x -= 3;   // $x = $x - 3  -> 12
$x *= 2;   // $x = $x * 2  -> 24
$x /= 4;   // $x = $x / 4  -> 6
$x %= 4;   // $x = $x % 4  -> 2

$str = "Hello";
$str .= " World"; // string concatenation assignment -> "Hello World"
echo $str;
?>

Comparison Operators

Used to compare two values. They return true or false. Note the difference between == (loose equality) and === (strict equality, checks type too).

Comparison Operators
<?php
$a = 5;
$b = "5";

var_dump($a == $b);   // true  (loose: values match)
var_dump($a === $b);  // false (strict: types differ)
var_dump($a != $b);   // false
var_dump($a !== $b);  // true
var_dump($a < 10);   // true
var_dump($a > 10);   // false
var_dump($a <= 5);   // true
var_dump($a >= 6);   // false

// Spaceship operator (PHP 7+): returns -1, 0, or 1
echo (1 <=> 2);  // -1
echo (2 <=> 2);  //  0
echo (3 <=> 2);  //  1
?>

Logical Operators

Used to combine conditional statements. && and and both mean AND, but && has higher precedence. Same for || vs or.

Logical Operators
<?php
$age = 25;
$hasID = true;

// AND: both must be true
if ($age >= 18 && $hasID) {
    echo "Access granted";
}

// OR: at least one must be true
$isAdmin = false;
$isModerator = true;
if ($isAdmin || $isModerator) {
    echo "Can manage content";
}

// NOT: inverts the boolean
if (!$isAdmin) {
    echo "Not an admin";
}

// XOR: true if exactly one operand is true
var_dump(true xor false); // true
var_dump(true xor true);  // false
?>

String & Increment/Decrement Operators

The . operator concatenates strings. ++ and -- increment or decrement a value by 1.

String & Increment/Decrement
<?php
// String concatenation
$first = "Hello";
$last  = "World";
echo $first . " " . $last; // Hello World

// Increment / Decrement
$count = 5;
echo $count++;  // 5 (post-increment: returns then increments)
echo $count;    // 6
echo ++$count;  // 7 (pre-increment: increments then returns)

$n = 10;
echo $n--;  // 10
echo --$n;  // 8
?>

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.