Statements: If

An if statement dictates some action when a given condition is true, and optionally when it is false. For example:

if ($count < 0)
    ...
  elseif ($count === 0)
    ...
  elseif ($count < 10)
    ...
  else
    ...

If the expression tests true, the single statement that follows immediately is executed. Otherwise, if an elseif clause is present the statement immediately following the elseif is executed. Otherwise, any other elseif expressions are evaluated. If none of those tests true, if an else clause is present at the end, the statement immediately following that is executed. The elseif and else clauses are optional.

See compound statements for a discussion about the use of braces around the statement body.

The expression whose value is tested can be arbitrarily complex, involving values and relational, equality, and logical operators, but it must have type bool or be implicitly convertible to that type. For example:

if (($i <= 10 && $j !== 0)) || !getStatus() {
  ...
}

An else clause is associated with the lexically nearest preceding if or elseif. For example:

if (...)
  ...
  if (...)
    ...
else  // this else does NOT go with the outer if
  ...

if (...) {    // note the addition of the braces
  ...
  if (...)
    ...
} else      // this else does go with the outer if
  ...