Jump to content

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'm in the boat with  pikachu and the old car.

Now that you mention being from a C background it made me realize, that style I notice mostly in OO languages.

 

Maybe we can look forward to a shift in style now that PHP is OO as well...

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 ;)

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.