Jump to content

'most correct' if statement syntax


The Letter E

Recommended Posts

I just had a curiosity that was bugging me and I'm unsure of how to google for the answer, so I thought I'd put it up to the test here.

 

I learned that when writing if statements in php the syntax is as follows:

if(condition){
    //Execute Code...
}else{
    //Execute Other Code...
}

 

However, I also see it written like so:

if(condition)
{
    //Execute Code...
}
else
{
    //Execute Other Code...
}

 

I thought that the later style was used for Javascript and the earlier for php. While they both work, I was wondering, which is more correct?

 

Thanks Everybody!

 

E

Link to comment
Share on other sites

For sure. You'd think that the manual would be "the way". I think it just gets shaky because line breaks aren't counted when the server processes the script, unless a \n is used, of course. :)

 

Thanks for the reply! If anyone else wants to chime in on this, I'm still curious to hear other peoples thoughts and/or relevant sources on this.

Link to comment
Share on other sites

It's a standards thing really.  Both work exactly the same.  I come from a C background and prefer:

if($var <= $my_mom)
{
}
else
{
}

 

The biggest reason I prefer this way is because I can then easily match up the curly brackets so I know where each clause ends because they're on the same indentation level. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.