[PHP] Proper code formatting
Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? if ($variable = 'this') { do this; and this; } else { if ($name = 'bill') { do something will bill; and something else with bill; } else { assume its not bill; and do the fred thing; } I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? if ($variable = 'this') { do this; and this; } else { if ($name = 'bill') { do something will bill; and something else with bill; } else { assume its not bill; and do the fred thing; } First - if ($variable = 'this') with assign the value this to variable and then return true. Probably not what you want ;) This is what I do: if ($variable == 'this') { do this; and this; } else { if ($name == 'bill') { do something with bill; and something else with bill; } else { assume its not bill; and do the fred thing; } } // note that you were missing a } - easy to miss with your style Of course - it is better like this if ($variable == 'this') { do this; and this; } elseif ($name == 'bill') { do something with bill; and something else with bill; } else { assume its not bill; and do the fred thing; } but what many like to do is something like this: if ($variable == 'this') { do this; and this; } elseif ($name == 'bill') { do something with bill; and something else with bill; } else { assume its not bill; and do the fred thing; } To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Proper code formatting
From: Michael A. Peters Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
Bob McConnell wrote: From: Michael A. Peters Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell There are various coding standards. There is one for PEAR, the Zend Framework and most frameworks/large projects that take contributions have them. Here's Zend: http://framework.zend.com/manual/en/coding-standard.html -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
On Mon, Mar 23, 2009 at 8:23 AM, Shawn McKenzie nos...@mckenzies.netwrote: Bob McConnell wrote: From: Michael A. Peters Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell There are various coding standards. There is one for PEAR, the Zend Framework and most frameworks/large projects that take contributions have them. Here's Zend: http://framework.zend.com/manual/en/coding-standard.html -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Being a greenhorn, I too can benefit from this thread. Is that to say, Shawn, that you personally find this (Zend) standard as good or better than the rest?
Re: [PHP] Proper code formatting
George Larson wrote: On Mon, Mar 23, 2009 at 8:23 AM, Shawn McKenzie nos...@mckenzies.netwrote: Bob McConnell wrote: From: Michael A. Peters Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell There are various coding standards. There is one for PEAR, the Zend Framework and most frameworks/large projects that take contributions have them. Here's Zend: http://framework.zend.com/manual/en/coding-standard.html -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Being a greenhorn, I too can benefit from this thread. Is that to say, Shawn, that you personally find this (Zend) standard as good or better than the rest? I actually just went through this wit ha group of people that come from all different levels and back grounds in regards to programing. Trying to decide whether to use spaces, or tabs, short hand or long hand... It took quite a bit of discussion before we arrived at an agreement... It really didn't matter what format we used as long as we stayed consistent throughout the file. In other words, if you are going to edit a file and it uses spaces instead of tabs, use spaces So absolutely, develop some standards if you are going to have multiple coders working on it... But they don't have to be set by someone else... Personally though, I go for readability it may at times take longer to write it out, but since we all type 500 words permit with 100% accuracy it won't be a problem right? ;) And then when you go back to the code in 6 months, a year, 2 years... It's still easily read able :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
Hi Angus, please, read this topic http://www.igorescobar.com/blog/2009/02/03/coding-standards/ I speak a little bit about Coding Standards. Igor Escobar systems analyst interface designer www . igorescobar . com On Mon, Mar 23, 2009 at 9:48 AM, Jason Pruim pru...@gmail.com wrote: George Larson wrote: On Mon, Mar 23, 2009 at 8:23 AM, Shawn McKenzie nos...@mckenzies.net wrote: Bob McConnell wrote: From: Michael A. Peters Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell There are various coding standards. There is one for PEAR, the Zend Framework and most frameworks/large projects that take contributions have them. Here's Zend: http://framework.zend.com/manual/en/coding-standard.html -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Being a greenhorn, I too can benefit from this thread. Is that to say, Shawn, that you personally find this (Zend) standard as good or better than the rest? I actually just went through this wit ha group of people that come from all different levels and back grounds in regards to programing. Trying to decide whether to use spaces, or tabs, short hand or long hand... It took quite a bit of discussion before we arrived at an agreement... It really didn't matter what format we used as long as we stayed consistent throughout the file. In other words, if you are going to edit a file and it uses spaces instead of tabs, use spaces So absolutely, develop some standards if you are going to have multiple coders working on it... But they don't have to be set by someone else... Personally though, I go for readability it may at times take longer to write it out, but since we all type 500 words permit with 100% accuracy it won't be a problem right? ;) And then when you go back to the code in 6 months, a year, 2 years... It's still easily read able :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
Well, I was pondering making a recommendation, of sorts. I work in an environment with various levels of coders, perhaps similar to your description. Currently, there are no standards that I have seen. We all are bringing our coding habits with us. I don't know if it is important that I like lower case variable names with underscores for spaces while the next guy likes each letter of a new word in uppercase. However, I can imagine it getting out of control as the code continues to grow. On Mon, Mar 23, 2009 at 8:48 AM, Jason Pruim pru...@gmail.com wrote: George Larson wrote: On Mon, Mar 23, 2009 at 8:23 AM, Shawn McKenzie nos...@mckenzies.net wrote: Bob McConnell wrote: From: Michael A. Peters Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell There are various coding standards. There is one for PEAR, the Zend Framework and most frameworks/large projects that take contributions have them. Here's Zend: http://framework.zend.com/manual/en/coding-standard.html -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Being a greenhorn, I too can benefit from this thread. Is that to say, Shawn, that you personally find this (Zend) standard as good or better than the rest? I actually just went through this wit ha group of people that come from all different levels and back grounds in regards to programing. Trying to decide whether to use spaces, or tabs, short hand or long hand... It took quite a bit of discussion before we arrived at an agreement... It really didn't matter what format we used as long as we stayed consistent throughout the file. In other words, if you are going to edit a file and it uses spaces instead of tabs, use spaces So absolutely, develop some standards if you are going to have multiple coders working on it... But they don't have to be set by someone else... Personally though, I go for readability it may at times take longer to write it out, but since we all type 500 words permit with 100% accuracy it won't be a problem right? ;) And then when you go back to the code in 6 months, a year, 2 years... It's still easily read able :)
Re: [PHP] Proper code formatting
At 4:19 PM +1000 3/23/09, Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? if ($variable = 'this') { do this; and this; } else { if ($name = 'bill') { do something will bill; and something else with bill; } else { assume its not bill; and do the fred thing; } Angus: The way I would (a personal choice) indent it would be: if ($variable = 'this') { do this; and this; } else { if ($name = 'bill') { do something will bill; and something else with bill; } else { assume its not bill; and do the fred thing; } However, that's not the way I would code it: First, I would use tabs, not spaces. Second, your if statements should be == and not = . Third, I don't like run-on if's -- I would use a switch statement instead: switch(1) { case $variable == 'this': do this; and this; break; case $name == 'bill': do something with bill; and something else with bill; break; default: assume its not bill; and do the fred thing; break; } The switch is easier for me to understand. YMMV. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
Bob McConnell wrote: To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell I just remembered why I started doing it the way I currently do it (earlier thread on this I had forgotten) - my desktop monitor went south, so I ended up using my laptop exclusively for a few months (money was tight) - an old Thinkpad with low ram, so X11 really taxes it, worked in console a lot. Work in a console and vertical space is of viewing value, hence putting the opening { at the end and not on new line of it's own, and it has stuck with me ever since. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
George Larson wrote: Well, I was pondering making a recommendation, of sorts. I work in an environment with various levels of coders, perhaps similar to your description. Currently, there are no standards that I have seen. We all are bringing our coding habits with us. I don't know if it is important that I like lower case variable names with underscores for spaces while the next guy likes each letter of a new word in uppercase. However, I can imagine it getting out of control as the code continues to grow. I was the former until I started dabbling with JavaScript in DOM2 - now I do the latter a lot, but *sigh* not consistently. Interestingly, because of wiki's I guess, I'm also using mod_rewrite to do the ThisIsMyWebPage style pages on the server (all files are lower case with proper extension, but mod_rewrite lets you give a better presentation file name for the viewer) I guess I'm easily corrupted ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Proper code formatting
Date: Mon, 23 Mar 2009 09:52:21 -0300 From: titiolin...@gmail.com To: pru...@gmail.com CC: george.g.lar...@gmail.com; nos...@mckenzies.net; php-general@lists.php.net Subject: Re: [PHP] Proper code formatting Hi Angus, please, read this topic http://www.igorescobar.com/blog/2009/02/03/coding-standards/ I speak a little bit about Coding Standards. And how did you expect us to understand the language your site is written in? _ Show them the way! Add maps and directions to your party invites. http://www.microsoft.com/windows/windowslive/products/events.aspx
Re: [PHP] Proper code formatting
Im brazilian, and i understand your language, why you dont undertand my ? Regards, Igor Escobar systems analyst interface designer www . igorescobar . com On Mon, Mar 23, 2009 at 11:10 AM, abdulazeez alugo defati...@hotmail.comwrote: Date: Mon, 23 Mar 2009 09:52:21 -0300 From: titiolin...@gmail.com To: pru...@gmail.com CC: george.g.lar...@gmail.com; nos...@mckenzies.net; php-general@lists.php.net Subject: Re: [PHP] Proper code formatting Hi Angus, please, read this topic *http://www.igorescobar.com/blog/2009/02/03/coding-standards/ * I speak a little bit about Coding Standards. And how did you expect us to understand the language your site is written in? -- See all the ways you can stay connected to friends and familyhttp://www.microsoft.com/windows/windowslive/default.aspx
RE: [PHP] Proper code formatting
Im brazilian, and i understand your language, why you dont undertand my ? Regards, Igor Escobar systems analyst interface designer www . igorescobar . com Well I guess the answer to that is obvious. I'm not brazilian and it would be so generous of you to use the most acceptable International language in the world (English). Though it's not my first language too but I use it as a mean to communicate with others. Atleast the essence of communication is to be understood isn't it? NOTE: I speak other international languages too though but I haven't come up to learning brazilian. Maybe you can help me with a simple tutorial on your language. Best regards. Alugo. _ Drag n’ drop—Get easy photo sharing with Windows Live™ Photos. http://www.microsoft.com/windows/windowslive/products/photos.aspx
Re: [PHP] Proper code formatting
Ok man, I'm sorry. This post i wrote in a few days ago in my native language. I dont have time enough to write in two languagesbut who knows in the future i can do it? I'm sorry about that man,but if you like the post anyway, try something like google translator. Regards, Igor Escobar systems analyst interface designer www . igorescobar . com On Mon, Mar 23, 2009 at 11:33 AM, abdulazeez alugo defati...@hotmail.comwrote: Im brazilian, and i understand your language, why you dont undertand my ? Regards, Igor Escobar systems analyst interface designer www . igorescobar . com Well I guess the answer to that is obvious. I'm not brazilian and it would be so generous of you to use the most acceptable International language in the world (English). Though it's not my first language too but I use it as a mean to communicate with others. Atleast the essence of communication is to be understood isn't it? NOTE: I speak other international languages too though but I haven't come up to learning brazilian. Maybe you can help me with a simple tutorial on your language. Best regards. Alugo. _ Drag n’ drop—Get easy photo sharing with Windows Live™ Photos. http://www.microsoft.com/windows/windowslive/products/photos.aspx
RE: [PHP] Proper code formatting
Ok man, I'm sorry. This post i wrote in a few days ago in my native language. I dont have time enough to write in two languagesbut who knows in the future i can do it? I'm sorry about that man,but if you like the post anyway, try something like google translator. Regards,Igor Escobar systems analyst interface designer www . igorescobar . com Well. Since it's not a common thing on this mailing list to hear someone say he's sorry I readily accept your apology and I'll use the google translator as you suggested. Come to think of it, arguments almost never end well on this list (we just broke the record). Remember the one between Almighty, self acclaimed Superstar Dan and Bot Jessica? then there was the one between the client who later became the employer and Tedd. Cheers. _ More than messages–check out the rest of the Windows Live™. http://www.microsoft.com/windows/windowslive/
Re: [PHP] Proper code formatting
George Larson wrote: On Mon, Mar 23, 2009 at 8:23 AM, Shawn McKenzie nos...@mckenzies.netwrote: Bob McConnell wrote: From: Michael A. Peters Angus Mann wrote: Hi all, I'm fairly new to PHP so I don't have too many bad habits yet. I'm keen to make my code easy to read for me in the future, and for others as well. Are there any rules or advice I can use for formatting (especially indenting) code? for example how best to format / indent this ? To each his own. Whatever floats your canoe. Just whatever you pick, stick to it throughout your code. I'm using PHP designer 2008 which does syntax coloring but if it has something to automatically indent - I haven't found it yet. It probably allows you to either set a specify a tab as a real tab or a specified number of spaces. Auto-indenting - this isn't python, the compiler doesn't enforce it's way, you follow the convention of the project you are working on - so I suspect many php editors tailored to php don't have an auto indent. I've never of course tried that specific product. I use bluefish, vi, and emacs. To take this question a step further, is there a PHP best practices document available? I am looking for one that I can give to a new programmer and tell her do it this way until you can explain to me why you shouldn't. Bob McConnell There are various coding standards. There is one for PEAR, the Zend Framework and most frameworks/large projects that take contributions have them. Here's Zend: http://framework.zend.com/manual/en/coding-standard.html -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Being a greenhorn, I too can benefit from this thread. Is that to say, Shawn, that you personally find this (Zend) standard as good or better than the rest? Many of them are very similar, I just picked Zend as an example because it is well known and they are the PHP company :-) I tend to follow many of the common standards but not any one in particular. I am not a professional developer and don't work with others really so its not as big of a deal. If you are working with others then I see that it is important first and foremost to have some consistency and readability. Some things are more personal preference and some are meant to make the code readable/portable. For example, the spaces vs. tabs is important because some systems/editors/IDEs display tabs differently, but 4 spaces should be 4 spaces almost everywhere. I use CakePHP mostly and try and follow they way it is coded so that it is consistent, but I flip flop on some things for no apparent reason sometimes without even realizing it. * I used to do this: function somefunc() { //something } * and if ($something) { //do something } else { //do something else } * Now I do this: function somefunc() { //something } if ($something) { //do something } else { //do something else } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Proper code formatting
Thx ab. I'm did not to be rude ;) Have a nice day. Regards, Igor Escobar systems analyst interface designer www . igorescobar . com On Mon, Mar 23, 2009 at 12:05 PM, abdulazeez alugo defati...@hotmail.comwrote: Ok man, I'm sorry. This post i wrote in a few days ago in my native language. I dont have time enough to write in two languagesbut who knows in the future i can do it? I'm sorry about that man,but if you like the post anyway, try something like google translator. Regards,Igor Escobar systems analyst interface designer www . igorescobar . com Well. Since it's not a common thing on this mailing list to hear someone say he's sorry I readily accept your apology and I'll use the google translator as you suggested. Come to think of it, arguments almost never end well on this list (we just broke the record). Remember the one between Almighty, self acclaimed Superstar Dan and Bot Jessica? then there was the one between the client who later became the employer and Tedd. Cheers. _ More than messages–check out the rest of the Windows Live™. http://www.microsoft.com/windows/windowslive/