1. What Is an Expression?
An expression is a combination of operands and operators. Operands can be constants, variables, or function calls, while operators define the action performed on those operands. Every expression evaluates to a single value.
For example, $a = 5 + 3; — here 5 and 3 are operands, + is the operator, and the whole expression evaluates to 8, which is then assigned to $a.
Assignment Operator
The assignment operator (=) takes the value on its right and stores it in the variable on its left. It is the most fundamental operator in PHP:
$name = "Learn PHP";
$count = 42;
Arithmetic Expressions
PHP supports standard arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These work as you would expect from mathematics.
2. Relational Operators
Relational (comparison) operators compare two values and return a boolean result — true or false. They are essential for conditional logic in your programs.
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | $a == $b |
!= or <> | Not equal | $a != $b |
> | Greater than | $a > $b |
< | Less than | $a < $b |
>= | Greater than or equal | $a >= $b |
<= | Less than or equal | $a <= $b |
3. Logical Operators
Logical operators combine multiple boolean expressions and return a boolean result. They are commonly used in if statements and loop conditions.
| Operator | Name | Result |
|---|---|---|
&& or and | AND | True if both operands are true |
|| or or | OR | True if at least one operand is true |
! | NOT | True if the operand is false |
Logical Operator Precedence
When combining logical operators, ! (NOT) has the highest precedence, followed by && (AND), and then || (OR). Use parentheses to make your intent explicit and avoid subtle bugs.
4. Operator Precedence
When an expression contains multiple operators, PHP evaluates them in a defined order. The general hierarchy, from highest to lowest priority, is:
- Parentheses
() - Arithmetic operators (
*,/,%before+,-) - Relational operators (
<,>,<=,>=,==,!=) - Logical operators (
!, then&&, then||) - Assignment operator (
=) — lowest precedence
When in doubt, wrap sub-expressions in parentheses to control evaluation order explicitly.
Expressions form the very foundation of any PHP script, acting as small building blocks that produce a single value. When you write code, nearly every line you type is an expression of some kind. A simple number like 42 is an expression that evaluates to 42. A variable like $name is an expression that evaluates to whatever value that variable holds. Even a function call is an expression, returning a value you can use immediately. Understanding expressions helps you think clearly about how data flows through your program. As you follow along with video tutorials, you will see how combining small expressions into larger ones allows you to perform complex calculations and data transformations with just a few lines of code.
Operators are the tools that let you act upon your data in meaningful ways. In PHP, operators are symbols that tell the interpreter to perform a specific mathematical, logical, or comparison action. The most familiar operator is the assignment operator, which stores a value into a variable. But PHP offers many more, including arithmetic operators for addition and subtraction, comparison operators for checking equality or ordering, and logical operators for combining conditions. Each operator works with operands—the values or variables it acts upon—to produce a result. Mastering these operators is essential because they appear in nearly every PHP script, from simple variable assignments to complex decision-making logic.
Arithmetic operators in PHP behave much like the math you already know from everyday life. You can add, subtract, multiply, and divide numbers with ease using the plus, minus, asterisk, and forward slash symbols. Beyond these basics, PHP also offers a modulus operator for finding remainders, and exponentiation for raising numbers to a power. These operators follow standard mathematical precedence rules, meaning multiplication and division happen before addition and subtraction unless you use parentheses to group operations explicitly. As you watch the tutorial videos, you will see how arithmetic operators are used in real-world scenarios, such as calculating totals in a shopping cart or processing numeric data from a form submission.
Comparison operators allow your PHP code to make decisions by comparing two values. Operators like equal, identical, not equal, greater than, and less than each return a boolean result—either true or false. This boolean result is what powers conditional statements like if and while, enabling your programs to respond differently depending on user input or other data. The distinction between equal and identical is particularly important in PHP: the double equals checks if values are the same after type conversion, while triple equals checks both value and type without conversion. Understanding these nuances helps you write more predictable and bug-free code as you progress from beginner to confident PHP developer.
Logical operators give you the power to combine multiple conditions into a single, more expressive check. Using operators like and, or, and not, you can build sophisticated decision-making logic that evaluates several criteria at once. For instance, you might check whether a user is both logged in and has permission to view a page before granting access. These operators short-circuit, meaning PHP stops evaluating as soon as the result is determined, which can improve performance and prevent errors. As you build your first complete application—such as a news board or content management system—you will rely heavily on logical operators to control program flow and ensure your application behaves correctly under varying conditions.
Learn PHP Tutorial