Obi1-Cannabis Posted August 23, 2010 Share Posted August 23, 2010 Hi,just for curiosity, i've allways used the syntax if(this == that){ //do something}else{ //do something else } But i'd like to know if it's a better practice to use if(this == that): //do somethingelse: //do something elseendif; if yes, why? Link to comment Share on other sites More sharing options...
chibineku Posted August 23, 2010 Share Posted August 23, 2010 The braces are canonical, i.e. best practice, mostly from a legibility point of view. It's way easier to see the structure if you use them. Link to comment Share on other sites More sharing options...
Obi1-Cannabis Posted August 23, 2010 Author Share Posted August 23, 2010 yes in terms of keeping the code cleanner and readable i guess you're right, i just tought there might be some performance differences.thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2010 Share Posted August 23, 2010 I don't think there's much of a performance difference, but the alternative syntax can be useful if you're doing things like writing out a bunch of HTML on a page. <?php if(this == that): ?>html herehtml herehtml herehtml herehtml herehtml here<?php else: ?>html herehtml herehtml herehtml herehtml herehtml herehtml here<?php endif; ?> That may look cleaner to you than this: <?php if(this == that) { ?>html herehtml herehtml herehtml herehtml herehtml here<?php } else { ?>html herehtml herehtml herehtml herehtml herehtml herehtml here<?php } ?> Link to comment Share on other sites More sharing options...
jeffman Posted August 23, 2010 Share Posted August 23, 2010 I think the key words in jsg's reply are "to you." This is really a matter of personal preference (or in-house style if you're corporate). I never worked with the alternative style in any C-type language before I came to PHP, so it does not look cleaner to me. I can see a seasoned VB programmer liking it, however. Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted August 24, 2010 Share Posted August 24, 2010 This is the fastest: <?phpif($blah === 'that') {// ...}else {// ...}?> You can also use switch statements: <?phpswitch($blah) { case 'that': // ... break; default: // ... break;}?> I would recommend the use of curly brackets as it makes it easier to read your code, and if you write more than one line of code, it works. Also, strict comparisons (using ===) are faster than value, or whatever they're called, comparisons (using ==). Link to comment Share on other sites More sharing options...
MrFish Posted August 24, 2010 Share Posted August 24, 2010 If you don't use braces you can only use one line.Ok- if(//true) //Do somethingelse //Do something else Not ok- if(//true) //Do something //Am I in the true statement? No.else //Do something else //Am I in the else statement? No. Link to comment Share on other sites More sharing options...
Ingolme Posted August 24, 2010 Share Posted August 24, 2010 If you don't use braces you can only use one line.Ok-if(//true) //Do somethingelse //Do something else Not ok- if(//true) //Do something //Am I in the true statement? No.else //Do something else //Am I in the else statement? No. By putting a colon after the if() you can add several lines. It's a syntax from BASIC-derived languagesif(condition): // Several // lineselse: // of // codeendif; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.