Share via:

PHP: Ifs, Colon and Semi-colon Style

How to use the alternative syntax of writing if statements in PHP to perform conditional execution of code.

110 views

Edited: 2019-12-15 18:55

Alternative syntax, PHP if statements.

In PHP, there is an alternative syntax for writing if statements when performing conditional execution of code, allowing us to use colon and semi-colon when writing the if statement.

This syntax is using endif keyword to finish the if statement.

A single if block will look like this:

if ('John' == $name):
  echo 'Hallo Rasmus :-)';
endif;

Including an else case in the statement:

if ('Zeev' == $name):
  echo 'Hallo Zeev :-)';
else:
  echo 'Sorry. I do not know you.';
endif;

Finally, we may also use the elseif keyword:

if ('Andi' == $name):
  echo 'Hallo Andi :-)';
elseif ('Rasmus' == $name):
  echo 'Hallo Rasmus!';
elseif ('Zeev' == $name):
  echo 'Hallo Zeev!';
endif;

Comments