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
https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/
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.

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. 

I like to stick to the ZF coding standards as much as possible. http://framework.zend.com/manual/en/coding-standard.coding-style.html

 

Of course there are a few changes that I make myself, but yeah. Having a standard will help you out later when you come back to your code to fix/tweak it ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.