๐Ÿ’ก Ask Tutor

Operators and Expressions in PHP

What are Operators in PHP?

Operators in PHP are symbols that perform operations on variables and values. These can be used to perform mathematical calculations, compare values, assign values, manipulate strings, and evaluate logic.

An expression in PHP is a combination of values, variables, and operators that returns a value.

1. Arithmetic Operators in PHP

These are used to perform basic mathematical operations.

OperatorDescriptionExampleResult
+Addition$x + $ySum of $x and $y
-Subtraction$x - $yDifference
*Multiplication$x * $yProduct
/Division$x / $yQuotient
%Modulus$x % $yRemainder

Example:

PHP
<?php
$x = 10;
$y = 3;

echo $x + $y; // Output: 13
echo "<br>";
echo $x % $y; // Output: 1
?>

2. Assignment Operators in PHP

Assignment operators assign values to variables.

OperatorMeaningExampleEquivalent To
=Assign$x = 55
+=Add and assign$x += 3$x = $x + 3
-=Subtract and assign$x -= 2$x = $x - 2
*=Multiply and assign$x *= 2$x = $x * 2
/=Divide and assign$x /= 2$x = $x / 2
%=Modulus and assign$x %= 2$x = $x % 2

Example:

PHP
<?php
$score = 10;
$score += 5;
echo $score; // Output: 15
?>

3. Comparison Operators in PHP

Used to compare two values and return a boolean (true or false).

OperatorDescriptionExampleReturns
==Equal$x == $ytrue if equal (ignores type)
===Identical$x === $ytrue if equal and same type
!= or <>Not equal$x != $ytrue if not equal
!==Not identical$x !== $ytrue if not same type or value
>Greater than$x > $ytrue if greater
<Less than$x < $ytrue if less
>=Greater than or equal$x >= $ytrue if greater or equal
<=Less than or equal$x <= $ytrue if less or equal

Example:

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

echo ($a == $b) ? "Equal<br>" : "Not Equal<br>";      // Equal
echo ($a === $b) ? "Identical" : "Not Identical";    // Not Identical
?>

4. Logical Operators in PHP

Logical operators are used to combine conditional statements.

OperatorDescriptionExampleResult
&& or andTrue if both are true$x && $yLogical AND
`oror`True if one is true
!Not!$xTrue if $x is false

Example:

PHP
<?php
$isAdmin = true;
$isLoggedIn = false;

if ($isAdmin && $isLoggedIn) {
    echo "Welcome, Admin!";
} else {
    echo "Access Denied!";
}
?>

5. String Operators in PHP

Used for working with and joining strings.

OperatorDescriptionExample
.Concatenation$a . $b joins two strings
.=Concatenation assignment$a .= $b appends right string to left

Example:

PHP
<?php
$first = "Hello ";
$second = "World!";
$full = $first . $second;

echo $full; // Output: Hello World!
?>

6. Operator Precedence in PHP

Operator precedence determines how expressions are evaluated in the absence of parentheses. PHP follows the standard math logic (BODMAS).

Example:

PHP
<?php
$result = 5 + 3 * 2;
echo $result; // Output: 11 (multiplication happens before addition)
?>

To force order, use parentheses:

PHP
$result = (5 + 3) * 2; // Now result is 16

Common Mistakes with Operators

  • Mixing assignment = with comparison ==
  • Forgetting parentheses in logical expressions
  • Confusing == with ===
  • Misusing .= instead of . in string joining

Best Practices

  • Always use === for type-safe comparisons
  • Wrap complex conditions in parentheses
  • Use meaningful variable names in expressions
  • Avoid deeply nested conditions when possible

Notes:

  • PHP supports a wide variety of operators: arithmetic, assignment, comparison, logical, and string
  • Operator precedence affects how expressions are evaluated
  • Use === for strict equality, !== for strict inequality
  • Use . to concatenate strings in PHP
  • Logical operators like &&, || are vital in control structures

Practice Tasks

Task 1: Calculate Final Price

PHP
$price = 100;
$discount = 20; // in %
$final = $price - ($price * $discount / 100);
echo "Final Price: $final";

Task 2: Login Checker

PHP
$isMember = true;
$isPaid = false;

if ($isMember && $isPaid) {
  echo "Access Granted";
} else {
  echo "Upgrade Required";
}

Task 3: String Combiner

PHP
$first = "PHP ";
$second = "Tutorial";
echo $first . $second; // Output: PHP Tutorial