I saw the following code snippet:
<?php
if(!empty($_POST)): // case I: what is the usage of the :
if(isset($_POST['num']) && $_POST['num'] != ''):
$num = (int)$_POST['num'];
....
if($rows == 0):
echo 'No';
else: // case II: what is usage of :
echo $rows.'Yes';
endif;
I would like to know what the usage of “:” in php code is.
This is the alternative syntax for control structures.
So
is equivalent to
This can come very handy when dealing with HTML. Imho, it is easier to read, because you don’t have to look for braces
{}and the PHP code and HTML don’t feel like mixed up. Example:I would not use the alternative syntax in “normal” PHP code though, because here the braces provide better readability.