0

Why do people use

if(condition) :
    /*do something*/
else:
    /*do something else*/
endif;

instead of

if(condition) 
{
    /*do something*/
} 
else
{
    /*do something else*/
}

Is one better than the other in any way? What should i get use to too?

Fahad Sohail
  • 1,818
  • 4
  • 21
  • 33
  • There is no better or worse, just different. A question of personal style. Some people find that the first version is easier to read when PHP is used embedded inside HTML. That's all. But it is kind of exotic, usually the second form is preferred and considered standard. – arkascha Apr 29 '17 at 05:46
  • Please refer http://stackoverflow.com/questions/2125066/is-it-bad-practice-to-use-an-if-statement-without-brackets – Sanchit Gupta Apr 29 '17 at 05:47
  • i think now, it just one of style of coding, some developers think : provide more readable code and another thinks {} is more readable. – Mubashar Iqbal Apr 29 '17 at 05:48
  • The version with : works very good (as in readable) when used directly in HTML. – Yoshi Apr 29 '17 at 05:49
  • it's more readable when dealing with PHP direct injection with HTML – Moustafa Elkady Apr 29 '17 at 05:57

1 Answers1

2

This falls in the category of coding style. Some people like to use brackets some like to use the ":end..." notation. The advantage to the latter is more towards using PHP conditionals and loops where there is output to HTML. For example,

//Pure PHP with brackets
<?php
if ($condition) {
   echo "output";
}?>

//PHP and HTML with brackets
<?php
if ($condition) {?>
   output
<?php } ?>

//PHP and HTML with ":end..." notation
<?php if ($condition):?>
   output
<?php :endif ?>

Some people prefer the last as the ":endif" gives a clue what opening construct to look for when matching control structures. The bracket could match any other bracket but an ":endif" can only match and "if" construct. You can use either it is up to your style.

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33