W3docs

endif

The "endif" keyword is a control structure in PHP that is used to mark the end of an "if" statement. In this article, we will explore the syntax and usage of

endif

The endif keyword is part of PHP's alternative syntax for control structures. Instead of wrapping the body of an if statement in curly braces ({ ... }), the alternative syntax opens the block with a colon (:) and closes it with endif;. This style was added specifically to make PHP read cleanly when it is interleaved with HTML in templates.

This article covers the syntax of endif, when to reach for it, and the gotchas that trip people up.

Syntax

A standard if block uses braces; the alternative syntax replaces the opening brace with : and the closing brace with endif;:

The PHP syntax of endif

// Brace syntax
if ($condition) {
  // code to be executed
}

// Alternative syntax — equivalent
if ($condition):
  // code to be executed
endif;

Two rules matter:

  • The condition line ends with a colon (:), not an opening brace.
  • The block is closed with endif, which must be followed by a semicolon (;). Omitting the semicolon is the most common cause of a parse error here.

The same pattern applies to the other control structures: endforeach, endfor, endwhile, and endswitch.

Examples

Let's look at some practical examples of how the endif keyword can be used:

Examples of PHP endif

<?php

// Example 1
$x = 5;
if ($x == 5):
  echo "The value of x is 5." . PHP_EOL;
endif;

// Output: The value of x is 5.

// Example 2
$x = 10;
if ($x > 5):
  echo "The value of x is greater than 5.";
else:
  echo "The value of x is less than or equal to 5.";
endif;

// Output: The value of x is greater than 5.

In these examples, we use the endif keyword to mark the end of an if statement. The alternative syntax also supports else and elseif branches within the same block, all of which close with a single endif;:

<?php

$score = 82;

if ($score >= 90):
  echo "Grade: A";
elseif ($score >= 80):
  echo "Grade: B";
elseif ($score >= 70):
  echo "Grade: C";
else:
  echo "Grade: F";
endif;

// Output: Grade: B

Note that with the alternative syntax you write elseif as a single word — else if (two words) is not valid in an alternative-syntax block.

Using endif in HTML templates

This is the reason endif exists. When PHP and HTML are mixed, a trailing } is easy to lose track of, while <?php endif; ?> reads almost like a closing tag and clearly states which structure it ends. Compare the two styles:

<!-- Hard to scan: brace floating in markup -->
<?php if ($loggedIn) { ?>
  <p>Welcome back!</p>
<?php } ?>

<!-- Self-documenting: alternative syntax -->
<?php if ($loggedIn): ?>
  <p>Welcome back!</p>
<?php endif; ?>

The alternative form keeps your templates readable, especially when blocks nest, which is why most legacy PHP templates (and many frameworks' built-in templating) rely on it.

Common gotchas

  • The semicolon after endif is mandatory. endif without ; raises a parse error.
  • Do not mix syntaxes in one block. You cannot open with { and close with endif, or open with : and close with }.
  • Use elseif, not else if. The two-word form is only allowed in the brace syntax.

Benefits

Using the endif keyword has several benefits, including:

  • Improved readability: The endif keyword makes control structures easier to read, especially in templates where HTML and PHP code are mixed.
  • Clearer structure: It provides a distinct, named closing marker for each control block, reducing the chance of mismatched braces in complex layouts.

Conclusion

The endif keyword lets you close an if block with a clear, named marker instead of a brace, which pays off most when PHP is embedded in HTML. To go deeper, see the full if / else / elseif guide, and explore the matching alternative-syntax closers endforeach, endfor, and endswitch.

Practice

Practice
Which of the following statements are true about the endif directive in PHP?
Which of the following statements are true about the endif directive in PHP?
Was this page helpful?