Soft blue gradient wash with a pale cyan glow in the upper left, calm and open in mood

PHP Basics

Operators and Expressions in PHP

Expressions are the building blocks of PHP code. This guide walks through what an expression is, the operators that combine its parts, and the order in which PHP evaluates them.

Start with expressions

1. What is an expression?

An expression is any combination of values, variables and operators that produces a result. The pieces an operator works on are called operands. A single value is already an expression; so is a calculation that mixes several operands together.

Because expressions return a value, they can be assigned to a variable, compared, or passed into a function. Understanding this makes the rest of the language easier to read.

Assignment operator

The assignment operator stores the value of the expression on its right into the variable on its left. Assignment is itself an expression, so the assigned value is also the result of the statement.

Arithmetic expressions

Arithmetic expressions combine numeric operands with the usual mathematical operators to produce a number. They are the most familiar form of expression and appear in almost every script.

Expressions and operators form the very foundation of PHP programming, much like the basic ingredients in a cherished family recipe. Every variable assignment, every comparison, every mathematical calculation you write is an expression that produces a value. Understanding how operators work together and in what order PHP evaluates them gives you the confidence to write clean, predictable code. As you progress through this tutorial, you will find that mastering these building blocks allows you to combine MySQL database interactions with object-oriented structures, ultimately building complete applications from scratch. This knowledge transforms you from a beginner into a capable developer, saving you from spending days sifting through outdated information and letting you learn at your own pace.

Carved linocut relief print in deep forest green and warm cream, showing abstract interlocking geometric shapes stacked like building blocks
Interlocking shapes, like operands joined by operators, form a single expression.

2. Relational operators

Relational operators compare two operands and return a true or false result. They are the basis of every condition and loop.

Comparison in practice
PurposeWhat it checks
EqualityWhether two operands hold the same value
InequalityWhether two operands differ
Greater / less thanWhich of two operands is larger or smaller
Greater / less or equalOrdering, including the case where both are equal

A comparison produces a boolean value, which is why relational operators pair naturally with if statements and loop conditions.

3. Logical operators

Logical operators join or invert boolean results, letting you build compound conditions out of simple comparisons.

  • And

    True only when both sides of the condition are true.

  • Or

    True when at least one side of the condition is true.

  • Not

    Reverses the truth value of a single expression.

Precedence of logical operators. When several logical operators appear in one condition, PHP evaluates them in a fixed order rather than strictly left to right. Grouping parts of a condition with parentheses keeps the intent clear and avoids surprises.

4. Operator precedence

Operator precedence decides which part of an expression is evaluated first. Higher-precedence operators bind more tightly, so an expression can produce a different result depending on the order applied.

  • Parentheses override the default order and are the clearest way to express intent.
  • Arithmetic operators follow the familiar mathematical order.
  • Relational operators are applied before logical operators.
  • Assignment is applied after the expression on its right has been evaluated.

When in doubt, add parentheses. The code stays readable and the evaluation order is explicit.