Control Flow

Control flow structures direct the way PHP execute the statements. For example, a loop repeats the same statements several times; a goto instruction jumps to another part of the code; a if-then statement applies a block of code or another, depending on a condition.

  • if

  • else

  • elseif/else if

  • Alternative syntax for control structures

  • while

  • do…while

  • for

  • foreach

  • break

  • continue

  • switch

  • match

  • declare

  • return

  • require()

  • include()

  • require_once()

  • include_once()

  • goto

Control flow structures can’t be called dynamically : they have to be hardcoded, or nested in a closure or other function.

<?php

if ($a === 1) {
     $b = 2;
} else {
     $b = 3;
}

?>

Documentation

See also PHP flow control

Related : Statement, Loops, Semicolon, If Then Else, While, Do While, For, Foreach, Break, Continue, Switch, Match, declare(), Return, Inclusions, Goto