Quick Fix Summary
Need a fast answer? PHP uses eight main operator types: arithmetic, assignment, comparison, increment/decrement, logical, string, array, and conditional assignment. Arithmetic handles math (e.g., +, -); logical operators return true/false (e.g., &&, ||); string operators concatenate text with .; array operators compare arrays. Use these in expressions like $a + $b or $x && $y to control logic and calculations.
What Are PHP Operators?
Think of them as the building blocks of PHP expressions. They let you do everything from basic math to complex logic checks. PHP organizes operators into eight distinct families—each one handles a specific job. Arithmetic operators crunch numbers, logical operators evaluate true/false conditions, and string operators join text together. The good news? Since PHP 8.x (as of 2026), these operators haven’t changed in their core behavior, though newer versions sometimes tweak edge cases.
How do I use PHP operators step by step?
Let’s break it down family by family. I’ll show you exactly how each one works with practical examples you can copy and test yourself.
- Arithmetic Operators
- Run calculations with
+,-,*,/,%(modulus), and**(exponentiation). - Example:
$sum = $a + $b;(Adds two numbers together)
- Run calculations with
- Assignment Operators
- Store values with
=, or update them in place using+=,-=,*=, and friends. - Example:
$a += 5;(Same as$a = $a + 5;)
- Store values with
- Comparison Operators
- Test relationships between values using
==,===,!=,!==,<,>,<=,>=. - Example:
if ($x === $y) { ... }(Checks both value and type)
- Test relationships between values using
- Increment/Decrement Operators
- Bump a variable up or down by 1 with
++and--. - Example:
$a++;(Post-increment) or++$a;(Pre-increment)
- Bump a variable up or down by 1 with
- Logical Operators
- Combine conditions using
&&(AND),||(OR), and!(NOT). - Example:
if ($a > 0 && $b < 10) { ... }(Both conditions must be true)
- Combine conditions using
- String Operators
- Stick text together with the
.operator. - Example:
$text = "Hello " . "World";(Results in "Hello World")
- Stick text together with the
- Array Operators
- Compare or merge arrays with
+(union),==,===,!=, etc. - Example:
$merged = $arr1 + $arr2;(Combines arrays)
- Compare or merge arrays with
- Conditional Assignment (Ternary)
- Write inline if-else logic with
?:. - Example:
$result = $age >= 18 ? "Adult" : "Minor";(Short and sweet)
- Write inline if-else logic with
Why isn’t my PHP operator working?
Don’t panic—operators follow strict rules. If something feels off, start here:
- Group operations with parentheses. Example:
$x = (5 + 3) * 2;instead of$x = 5 + 3 * 2;(multiplication happens first otherwise). - Turn on error reporting. Add
error_reporting(E_ALL);andini_set('display_errors', 1);at the top of your script. This catches undefined variables and type mismatches. - Check your data types. Use
gettype()orvar_dump()to confirm variables are numbers or strings before running calculations.
How can I prevent common PHP operator mistakes?
Honestly, most bugs come from loose comparisons and unclean data. Here’s how to stay safe:
- Go strict. Use
===and!==instead of==and!=to avoid type juggling surprises—especially with user input. - Clean your inputs. Always validate and sanitize data before plugging it into arithmetic or logical expressions.
- Pick a style and stick with it. Mixing
and/orwith&&/||in the same project can get confusing. They do the same thing but have different precedence rules. - Comment complex lines. If you’re mixing arithmetic, logical, and ternary operators in one line, add a note explaining what’s happening.
For the full technical details, check the PHP Manual’s Operators documentation maintained by the PHP Group.