Skip to main content
The case keyword serves two distinct structural roles in PHP: as a branching label within a switch statement to define conditional execution paths, and as a declaration operator within an enum (PHP 8.1+) to define discrete enumeration members.

Switch Statement Mechanics

  • Loose Comparison: Within a switch block, case relies on loose equality (==). Type coercion occurs during evaluation; the PHP engine does not enforce strict type equivalence (===) between the switch expression and the case expression.
  • Fall-through Control Flow: When a case condition evaluates to true, the PHP engine transfers the instruction pointer to that label. Execution proceeds sequentially downward, ignoring subsequent case evaluations, until it encounters a control structure modifier (break, return, or exit) or reaches the end of the block.
  • Dynamic Evaluation: PHP evaluates switch case expressions dynamically at runtime. A case can contain variables, mathematical operations, or function calls.
  • Label Stacking: Multiple case labels can be declared sequentially without intervening statements. This routes multiple distinct matches to a single execution block via intentional fall-through.
  • Terminator Syntax: The standard terminator for a switch case label is a colon (:), but PHP’s parser also accepts a semicolon (;).

Enum Declaration Mechanics (PHP 8.1+)

  • Singleton Instantiation: Within an enum construct, case defines a discrete, singleton instance of that enumeration type.
  • Backed Values: A case can optionally be assigned a scalar equivalent (string or integer) in a Backed Enum using the assignment operator (=).

Syntax Visualization

Evaluation Order (Switch Context)

The PHP engine evaluates switch case clauses sequentially from top to bottom. The first case that satisfies the loose comparison halts further condition evaluation. If a dynamic expression within a case contains side effects (e.g., modifying a variable or executing a database query), those side effects will only occur if the engine evaluates that specific case before finding a match.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More