2

I like to use the syntax with ":" and the "end-stuff".

if($var=='patate'):
   echo 'excellent';
else :
   ...
endif;

I know IDE are used to the {} syntax when it's time to collapse code, but other than that, is there any reason to NOT use this ":" syntax ?

2
  • 3
    This has been asked before, and I'll find the duplicate to link in, but basically it's up to preference except that most projects' coding standards use the proper {} syntax. The short syntax is designed for use in template files when you have a lot of PHP to embed inside HTML. Commented May 6, 2012 at 19:27
  • The user comments on Alternative syntax for control structures might give you some idea. Commented May 6, 2012 at 20:35

3 Answers 3

4

Don't use it. It's ugly and people usually expect the curly-braces syntax they are used to. It's most common when mixing PHP and HTML in the same file (i.e. when using PHP as a template engine) - but you shouldn't do that anyway, at least not if the same file also contains your application logic.

Autoindent scripts will also have trouble indenting your code properly since they usually just know one curly brace = one more/less level of indentation.

However, if you do like : instead of { have a look at python. :)

Sign up to request clarification or add additional context in comments.

Comments

1

The alternate syntax is of occasional use if you're in the habit of mixing PHP and HTML blocks, e.g.

<?php if ($something): ?>
<h2>blah blah blah</h2>
<?php endif; ?>

But if you're using a decent syntax-highlighting editor, there's pretty much no point in using it, as the colorization will give you all the contextual hints you need as to what's PHP and what's HTML. And editors like VIM which let you "bounce" on brackets/braces also negate the need - you can't bounce on an 'endif'.

2 Comments

It is far more convenient, shorter and readable if you use {} in the above example, why would someone still use the :?
compatibility with an old code base, most likely. I'd agree that only {} should be used in anything new. Alternate syntax ranks right up there with magic_quotes and register_globals for things that should be eliminated from PHP (and thankfully, 5.4 did just that with magic quotes and register globals).
0

The main reason that people are against using the alternate syntax is because some server configurations may not allow it. If you don't control your server environment or need to share code with others, then it may be best to shy away from using the alternate syntax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.