Alternative Syntax

Use colon : and endif; instead of using curly braces { and }

 

Use corresponding endXXX ; for opening control structure, replacing XXX as follows:

endif ;

endwhile ;

endfor ;

endforeach ;

endswitch ;

 

Also applies to else : and elseif :

 

<?php
	if (TRUE) :

		echo "TRUE is always TRUE" ;

	else :

		echo "TRUE is never FALSE" ;

	endif;
?>

 

Another simple example that only displays the the HTML if the test condition is TRUE

<?php if ($myVar == "Potato") : ?>

<p>$myVar is Potato</p>

<?php endif; ?>

 

A variation of the Mixed mode example:

<?php
	$myVar = rand(1,2) ;

	if ($myVar == 1) :
?>

	<h1>Conditional HTML</h1>
	<p>This will only be displayed if the above PHP 'if'<br>
	test expression is TRUE!</p>

<?php
	else :
?>

	<h1>More Conditional HTML</h1>
	<p>This will only be displayed if the above PHP 'if'<br>
	test expression is FALSE!</p>

<?php
	endif ;
?>

Leave a Reply