PHP Question! 'Cus I'm A Self Taught Hack.

6 replies
What is the difference between...
<?php if stuff : ?>

stuff

<?php else: ?>

stuff

<?php end if; ?>
-- And This --
<?php if stuff { ?>

stuff

<?php } else { ?>

stuff

<?php } ?>
Both work the same but I'm sure there is a reason to use one or the other, I just don't know what it is.
#cus #hack #php #question #taught
  • Profile picture of the author limcode
    There is no technical difference between the two syntaxes. I used to use the curly braces but now a days I prefer to use this short-hand alternative syntax because of code readability and accessibility. And especially, If you want to use PHP as your templating language in your view files(the V of MVC), the first one is better.
    {{ DiscussionBoard.errors[3326935].message }}
  • Profile picture of the author SteveJohnson
    The only reason you might find it easier to use the curly brace syntax is that many code editors will match beginning and ending braces to show you where that particular code block starts and stops. This can be useful in debugging.

    If you use the alternate syntax and have a bunch of endif; statements floating around, you're never sure which if statement they match up with.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3334528].message }}
  • Profile picture of the author phpbbxpert
    The standard is using curly braces for scripting.

    The only time if $foo: end if; type syntax is generally used is in some templating systems like WordPress's.

    Otherwise braces are almost always used.
    {{ DiscussionBoard.errors[3335721].message }}
  • Profile picture of the author wayfarer
    Personally I find the whole if(): endif; thing rather annoying, and never use it. And my IDE highlights the opening and closing brackets as pointed out above, so those are much better for my purposes. There's no technical difference however
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[3336416].message }}
  • Profile picture of the author jasonmorgan
    thanks guys.

    and yeah, my introduction to php was wordpress so I'm a little brainwashed by how they did things.
    Signature

    I'm all about that bass.

    {{ DiscussionBoard.errors[3337965].message }}
    • Profile picture of the author wayfarer
      Originally Posted by jasonmorgan View Post

      thanks guys.

      and yeah, my introduction to php was wordpress so I'm a little brainwashed by how they did things.
      I think it all comes down to a matter or preference. Do whatever is the most comfortable to you. As a matter of practice, it is better to use curly braces {} when not breaking from PHP into HTML code. When doing "template like" code (as wordpress templates) it's considered to be okay to use if(): endif; etc...
      Signature
      I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
      {{ DiscussionBoard.errors[3340618].message }}

Trending Topics