The Letter E Posted December 16, 2010 Share Posted December 16, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/ Share on other sites More sharing options...
sstangle73 Posted December 16, 2010 Share Posted December 16, 2010 the php manual has if(){ } http://php.net/manual/en/control-structures.if.php but the commenters have both. I think it is probably just a preference for easibility to read Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/#findComment-1148410 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 It's just a matter of preference. I find it easier to read code like this: if( ) { //do something } else { // so something else } Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/#findComment-1148411 Share on other sites More sharing options...
The Letter E Posted December 16, 2010 Author Share Posted December 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/#findComment-1148412 Share on other sites More sharing options...
QuickOldCar Posted December 16, 2010 Share Posted December 16, 2010 I also like it better like this. I seem to be able to find the end brackets better all being on the one line. if ($condition) { $do_something } else { $do_something_else } Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/#findComment-1148415 Share on other sites More sharing options...
PnzrDrgoon Posted December 16, 2010 Share Posted December 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/#findComment-1148416 Share on other sites More sharing options...
The Letter E Posted December 16, 2010 Author Share Posted December 16, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/#findComment-1148423 Share on other sites More sharing options...
Philip Posted December 16, 2010 Share Posted December 16, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221923-most-correct-if-statement-syntax/#findComment-1148426 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.