|
Best Practices: PHP Coding Style
Indenting
All your code should be properly indented. This is the most fundamental
thing you can do to improve readability. Even if you don't comment your
code, indenting will be a big help to anyone who has to read your code
after you.
while ($x < $z) {
if ($a == 1) {
echo 'A was equal to 1';
} else {
if ($b == 2) {
//do something
} else {
//do something else
}
}
}
The PEAR RFC standard calls for 4 spaces, not tabs of any size, in your code.
I disagree with this personally and will continue to tab my code. My view is
that tabs rather than spaces will create smaller files and smaller files are
faster to parse, upload, download, etc etc. The other advantage to using tabs
is that you can set your tab size to your personal preference when viewing
someone else's code. I used to use 8-space tabs, but recently switched to
4-space tabs and all my code "reformatted" automatically by just setting a
preference in vim.
Control Structures
This is pretty much common sense, but I still see an awful lot of code
that is not braced for readability. If you use conditional expressions (IF statements)
without braces, not only is it less readable, but bugs can also be introduced when
someone modifies your code.
Bad Example:
if ($a == 1) echo 'A was equal to 1';
That's pretty much illegible. It may work for you, but the person following
after you won't appreciate it at all.
Less Bad Example:
if ($a == 1) echo 'A was equal to 1';
Now that's at least readable, but it's still not maintainable. What if I want
an additional action to occur when $a==1? I need to add braces, and if I forget
to, I'll have a bug in my code.
Correct:
if (($a == 1) && ($b==2)) {
echo 'A was equal to 1';
//easily add more code
} elseif (($a == 1) && ($b==3)) {
//do something
}
Notice the space after the if and elseif - this helps distinguish
conditionals from function calls.
[ Next Page ]
| Comments: | ||
| Content management question | Rocky | 05/31/07 03:23 |
| In need for code | Michael | 02/10/05 08:18 |
| Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä! | ÀÌÇýÁø | 12/07/04 02:40 |
| ½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ý | ÀÌÈñÁø | 12/05/04 06:06 |
| Ä«,µå,¿¬,ü,ÀÚ/¿¹.Á¤.ÀÚ ´ë,Ãâ 100-1000¸¸¿ø | ÀÌ´ÙÇö | 12/05/04 01:43 |
| ´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ý | ÇѰæ¿í | 12/04/04 10:57 |
| ½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ý | ÀÌÈñÁø | 11/24/04 06:06 |
| ½Å.¿ë.ºÒ.·®/Ä«.µå.¿¬.ü/´ë.Ãâ/È¥ÀÚ/ÇØ.°áÇÏ´Â/¹æ.¹ý | ±èÇö¼ | 11/24/04 00:34 |
| ´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ý | ÇѰæ¹Î | 11/23/04 06:29 |
| ½Å.¿ëºÒ.·®ÀÚ°¡ ¾Ë¾Æ¾ßÇÒ Á¤.º¸ ´ë.°ø.°³ | ÀÌ´ÙÁø | 11/21/04 21:30 |
| Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä! | ÀÌÁ¤¿¬ | 11/18/04 09:48 |
| Ä«.µå °ª.¿ø.±Ý.¸¸.°±.´Â.¹æ.¹ý(°.Ãß) | Áö¿µÈñ | 11/14/04 19:44 |
| Coding Standards and Consistancy | Bruce Painter | 09/12/04 01:48 |
| Use tabs?!? What the ?? | thujone | 04/29/04 11:38 |
| Ä«µå¿¬Ã¼/½Å¿ëºÒ·®ÀÚ¿¡°Ô ²À ÇÊ¿äÇÑ Á¤º¸!!! | ¼ÇöÈñ | 01/06/04 23:33 |
| ½Å¿ëºÒ·®/Ä«µå¿¬Ã¼/È¥ÀÚÇØ°áÇÏ´Â ¹æ¹ý!!(Çʵ¶) | µµ¿ì¹Ì | 09/29/03 22:38 |
| my preference on indenting/curlies | gLaNDix | 08/07/03 13:02 |
| Bracing style | Avi Marcus | 07/18/03 01:01 |
| strings | Tom | 04/16/03 06:13 |
| RE: Tabs Cause Problems | Leon | 03/26/03 06:49 |
| RE: Braces should be on their own line, proba | Eric Kok | 01/09/03 08:35 |
| RE: Braces should be on their own line, proba | /zureash | 01/02/03 11:45 |
| RE: Using braces properly -- DON'T!! | Eugene Wee | 10/28/02 21:18 |
| RE: Braces should be on their own line, probably. | Drew | 10/22/02 15:27 |
| Suggestion | Daniel | 09/17/02 04:24 |
| RE: ALL | opinion | 08/12/02 13:22 |
| RE: nice style | sveno | 07/21/02 06:54 |
| RE: PHP for beginners | Nick | 07/20/02 11:55 |
| RE: people, get it thru your head | James Young | 07/11/02 12:12 |
| RE: setting indent spaces in vim | Steve Sutton | 07/11/02 09:37 |
| RE: PHP for beginners | sam | 07/04/02 04:49 |
| RE: people, get it thru your head | Adam Ellis | 07/02/02 14:22 |
| nice style | Pete | 07/02/02 14:18 |
| setting indent spaces in vim | Dave | 07/01/02 16:07 |
| strings and quotes - some test results | maren | 06/26/02 09:06 |
| newbieee | Vikram | 05/22/02 04:43 |
| Coding style examples are "CORRUPT" | Razzbar | 05/15/02 20:42 |
| Micro$oft bug free? | coditza | 05/14/02 19:42 |
| abt codding style | larry | 05/10/02 10:11 |
| This looks better | Matt | 04/25/02 14:50 |
| RE: people, get it thru your head | Razzbar | 04/21/02 17:54 |
| Yet Another Opinion | Mark | 04/11/02 16:06 |
| Dirty code - not only PHP | James Linden | 04/07/02 21:10 |
| send forum | lialy | 04/05/02 16:31 |
| RE: How about aligning those curly braces? | olabri | 03/25/02 08:36 |
| RE: Learning PHP | Alex Davies | 03/24/02 07:37 |
| Referencing a single image-path | Brian | 03/19/02 21:18 |
| RE: Special characters in text files | Jan | 02/21/02 21:06 |
| proper uses of include and include_once | Chris Travers | 02/18/02 21:05 |
| Now we see the real from the wanna-be | Jackansi | 02/06/02 10:07 |
| RE: PHP Hurts my head... | Kevin | 01/31/02 22:47 |
| RE: people, get it thru your head | Kevin | 01/31/02 22:45 |
| RE: <? PHP Sucks ?> | Kevin | 01/31/02 22:43 |
| RE: My Style | Peter Daemen | 01/28/02 09:42 |
| RE: Style/brackets ect...... | Peter Daemen | 01/28/02 09:39 |
| RE: people, get it thru your head | thomas whalen | 01/27/02 03:51 |
| <?php [php???] ?> | Qwik | 01/24/02 18:27 |
| PHP Hurts my head... | Steven | 01/21/02 19:49 |
| RE: people, get it thru your head | Nicole | 01/16/02 11:22 |
| RE: <? PHP Sucks ?> | Nicole | 01/16/02 11:06 |
| Special characters in text files | Owen Lysaght | 01/15/02 19:13 |
| RE: <? PHP Sucks ?> | Rod K | 01/14/02 22:58 |
| RE: <? PHP Sucks ?> | kr0m3 | 01/14/02 10:47 |
| RE: <? PHP Sucks ?> | Cory | 01/12/02 17:35 |
| RE: <? PHP Sucks ?> | Jean | 01/04/02 00:26 |
| RE: <? PHP Sucks ?> | Daniel Perry | 12/30/01 15:14 |
| Other probs with ASP tags etc - compatibility | Daniel Perry | 12/30/01 14:41 |
| Style/brackets ect...... | Peter | 12/17/01 00:59 |
| RE: <? PHP Sucks ?> | Peter | 12/17/01 00:50 |
| Using braces properly -- DON'T!! | Mike | 12/10/01 07:42 |
| RE: My Style... | Pacmon | 11/27/01 10:08 |
| RE: people, get it thru your head | NealC | 11/22/01 11:42 |
| RE: How about aligning those curly braces? | RChaneau | 11/21/01 12:09 |
| Creating and Commenting If Statements | Mihkel Karu | 11/04/01 14:23 |
| My Style | DKMan | 10/11/01 10:01 |
| RE: Becoming a Great Webmaster | Knight | 10/08/01 22:28 |
| RE: Becoming a Great Webmaster | Luciano ES | 10/08/01 10:43 |
| How about aligning those curly braces? | Luciano ES | 10/08/01 10:11 |
| My Style | Jonatas | 09/24/01 10:14 |
| RE: <? PHP Sucks ?> | Shane A | 09/19/01 19:20 |
| RE: <? PHP Sucks ?> | Patrick Altman | 09/10/01 20:37 |
| RE: <? PHP Sucks ?> | Micheal | 08/31/01 09:04 |
| My Style... | c0d34w4y | 08/25/01 17:02 |
| My Style | Dustin | 08/22/01 20:53 |
| RE: Interesting... | jimbarry | 08/22/01 13:04 |
| php editor | memoir | 08/14/01 20:09 |
| RE: <? PHP Sucks ?> | MikeQ | 08/11/01 12:14 |
| RE: php reformatters? | Carl Heaton | 07/26/01 07:34 |
| More on If Statements | Adam Sharp | 07/23/01 18:10 |
| RE: My style.. | Abraham Godong | 07/19/01 00:31 |
| Where????? | Fadi Aboualfa | 07/16/01 11:04 |
| beginning with php | sgelinas | 06/20/01 13:27 |
| RE: <? PHP Sucks ?> | Mike Hall | 06/18/01 02:27 |
| RE: php & html | yl828 | 06/10/01 01:37 |
| RE: <? PHP Sucks ?> | Dolce | 06/08/01 01:18 |
| Looks good but.... | Jeff | 06/07/01 09:12 |
| RE: PHP for beginners | Anne | 06/04/01 04:10 |
| RE: Including Code (no once) | Eric Blade | 05/30/01 00:15 |
| php & html | peppe | 05/17/01 06:31 |
| RE: Including Code (no once) | Miguel Cruz | 04/21/01 23:42 |
| RE: PHP for beginners | Eric Blade | 04/13/01 23:36 |
| RE: ASP tags OK | Eric Blade | 04/13/01 23:35 |
| RE: PHP for beginners | Erika Pimmer-Schniedermeier | 04/09/01 15:03 |
| One more comment | Dean Hall | 04/07/01 23:20 |
| General comments on PEAR | Dean Hall | 04/07/01 23:18 |
| ASP tags OK | Dean Hall | 04/07/01 23:11 |
| single quotes vs double quotes and strings | Eric Blade | 03/26/01 12:46 |
| RE: Checkboxes | boese | 03/24/01 04:33 |
| Where can I find some short Php docs ? | Chris | 03/23/01 11:38 |
| Including Code (no once) | Eldes :: MZO Interativa | 03/23/01 09:56 |
| braces and indentation | crankydoodle | 03/21/01 01:59 |
| RE: Checkboxes | Patricio Anguita | 03/19/01 17:30 |
| RE: Tabs | Marc | 03/16/01 13:20 |
| RE: HTML and PHP | Dave Kingdon | 03/10/01 10:23 |
| Shopping card prog with PHP | Gökmen CIFTCI | 03/10/01 08:31 |
| RE: Becoming a Great Webmaster | Plutarck | 03/10/01 00:31 |
| RE: ASP tags are discouraged?? HOLD IT! | SonOfDad | 03/09/01 10:26 |
| RE: PHP for beginners | SonOfDad | 03/09/01 10:20 |
| PHP for beginners | PHPwannabe | 03/08/01 14:43 |
| RE: Differences in quotes | philip olson | 03/05/01 21:46 |
| RE: ASP tags are discouraged?? HOLD IT! | Stephen Sandison | 03/02/01 00:55 |
| OTB again?! | Tad Marko | 03/01/01 08:37 |
| RE: Old Challenges Renewed (Hey, Kyle?) | Bogs | 02/28/01 09:54 |
| Checkboxes | shiv shankar | 02/27/01 03:39 |
| Problem send greeting card | alia | 02/08/01 23:11 |
| in sum? | OOZC | 02/08/01 14:17 |
| speed? | Nass | 02/06/01 03:03 |
| Differences in quotes | Keegan Miller | 02/04/01 23:12 |
| RE: SQL Statements | Keegan Miller | 02/04/01 23:01 |
| My style.. | ellis of geeknews.com | 01/30/01 04:52 |
| Coding Style | Andrew Tejero | 01/29/01 23:13 |
| How to do this? | ColdFusion | 01/27/01 15:47 |
| RE: Braces should be on their own line, probably. | Carlos Viana | 01/27/01 06:13 |
| RE: HTML and PHP | Carlos Viana | 01/27/01 06:01 |
| RE: ASP tags are discouraged?? HOLD IT! | Giovanni Moretti | 01/26/01 03:30 |
| people, get it thru your head | Ron McKown | 01/20/01 01:07 |
| ANSI C | Mundi King | 01/19/01 15:15 |
| RE: ASP tags are discouraged?? | Peter Be | 01/18/01 04:48 |
| RE: ASP tags are discouraged?? HOLD IT! | golem | 01/17/01 19:34 |
| RE: HTML and PHP | good voice | 01/16/01 06:02 |
| RE: Code Style - The Right Way:) | good voice | 01/16/01 05:49 |
| RE: Code Style - The Right Way:) | Henry | 01/15/01 18:33 |
| Double quotes | Devendra | 01/15/01 07:07 |
| RE: <? PHP Sucks ?> | Gilbert Rehling | 01/14/01 11:39 |
| RE: Becoming a Great Webmaster | paul | 01/13/01 16:43 |
| <? PHP Sucks ?> | John | 01/12/01 13:50 |
| Becoming a Great Webmaster | C. Carter | 01/11/01 11:35 |
| Code Style - The Right Way:) | Paul K Egell-Johnsen | 01/11/01 07:38 |
| HTML and PHP | Simen Brekken | 01/11/01 04:32 |
| String | BiTDaemon | 01/10/01 21:40 |
| RE: Paul's, Brian's, and Morpheus' posts | Anthony Boyd | 01/10/01 01:10 |
| if...then statements | mike | 01/09/01 22:35 |
| RE: A pariah perhaps | Björn Brändewall | 01/09/01 17:33 |
| RE: line wrapping | Björn Brändewall | 01/09/01 17:12 |
| VIM Setup for Good Form | Rex Byrns | 01/08/01 14:27 |
| TEMPLATES TEMPLATES TEMPLATES! | Benjamin Smith | 01/07/01 00:41 |
| Also about tags... | Henry | 01/06/01 19:46 |
| RE: Commenting Brackets | Anna | 01/06/01 00:49 |
| [PHP-PEAR] RFC: coding standards document | philip olson | 01/05/01 23:02 |
| Control structures | Shanx | 01/05/01 10:26 |
| RE: Paul's, Brian's, and Morpheus' posts | morpheus() | 01/05/01 06:33 |
| RE: Old Challenges Renewed (Hey Kyle) | morpheus() | 01/05/01 06:27 |
| RE: 2 quibbles: Indenting and brackets | Kelli | 01/05/01 01:05 |
| RE: Function/Variable naming | Wes Maldonado | 01/04/01 17:51 |
| ASP tags are discouraged?? HOLD IT! | Brett Tesdall | 01/04/01 17:48 |
| open and close curlies | Scott Marlowe | 01/04/01 12:43 |
| RE: Braces should be on their own line, probably. | Kevin Clark | 01/04/01 10:10 |
| RE: A pariah perhaps (Ericson Smith) | morpheus | 01/04/01 03:51 |
| Function/Variable naming | Peter Feiner | 01/03/01 20:25 |
| RE: Braces should be on their own line, probably. | Ara | 01/03/01 13:43 |
| Braces should be on their own line, probably. | Thomas Hurst | 01/03/01 09:15 |
| RE: Old Challenges Renewed | Thomas Hurst | 01/03/01 09:07 |
| RE: php reformatters? | Cecil A. Galbraith | 01/03/01 07:56 |
| What about effecient coding??!!? | Trond | 01/03/01 07:41 |
| RE: Tabs vs. Spaces | Roel | 01/03/01 06:08 |
| RE: SQL Statements | Jan Lehnardt | 01/03/01 05:59 |
| RE: SQL Statements | kjj | 01/03/01 02:24 |
| RE: Religion and Programming | Chris Lee | 01/02/01 20:58 |
| RE: Browsers and spaces | Vincent Vollers | 01/02/01 20:41 |
| HIT ME | nat | 01/02/01 18:24 |
| RE: php reformatters? | ff3j | 01/02/01 17:55 |
| Browsers and spaces | Erik J. Barzeski | 01/02/01 17:54 |
| Religion and Programming | Tom Anderson | 01/02/01 17:00 |
| Tabs ;-) | Tim Perdue, PHPBuilder.com | 01/02/01 16:12 |
| GNU Style | Charlie Collins | 01/02/01 16:08 |
| RE: Tabs vs. Spaces | Charlie Collins | 01/02/01 16:04 |
| RE: Paul's, Brian's, and Morpheus' posts | Anthony Boyd | 01/02/01 15:33 |
| php reformatters? | hollins | 01/02/01 14:54 |
| RE: Tabs vs. Spaces | Bryan Brunton | 01/02/01 14:48 |
| INDENTING... What I prefer to read : | André | 01/02/01 14:34 |
| it's about consistency | Stephen VanDyke | 01/02/01 14:05 |
| That's really not the 1/4 of it! | Ken Egervari | 01/02/01 13:29 |
| RE: A pariah perhaps | Tom Anderson | 01/02/01 13:28 |
| tip about comments | Ricardo Leite | 01/02/01 13:13 |
| Old Challenges Renewed | Kyle | 01/02/01 12:50 |
| Old Challenges Renewed | Kyle | 01/02/01 12:50 |
| RE: A pariah perhaps | Ericson Smith | 01/02/01 12:42 |
| Devils advocate | Richard Wallace | 01/02/01 11:39 |
| Includes | Erik J. Barzeski | 01/02/01 11:13 |
| RE: line wrapping | luke chasteen | 01/02/01 11:06 |
| RE: Naming conventions | luke chasteen | 01/02/01 10:51 |
| RE: A pariah perhaps | luke chasteen | 01/02/01 10:49 |
| RE: It is easy to get a good coding style | Jason Lee | 01/02/01 10:25 |
| Great Tim | Fabrizio | 01/02/01 10:25 |
| A proposed "grammar" for comments | Ganesh Prasad | 01/02/01 10:10 |
| RE: line wrapping | Chris Newman | 01/02/01 09:42 |
| RE: indent | Tim Wasson | 01/02/01 08:57 |
| Braces | Pablo | 01/02/01 08:04 |
| OO / Database / SQL Addition | Vincent Vollers | 01/02/01 07:28 |
| RE: A pariah perhaps | Vincent Vollers | 01/02/01 07:27 |
| SQL Statements | Anderson Fortaleza | 01/02/01 07:16 |
| RE: Commenting Brackets | WayneH | 01/02/01 07:08 |
| RE: It is easy to get a good coding style | Glen Scott | 01/02/01 06:44 |
| A pariah perhaps | morpheus() | 01/02/01 05:51 |
| Naming conventions | Jonathan Kahn | 01/02/01 05:45 |
| It is easy to get a good coding style | Lars Dybdahl | 01/02/01 04:24 |
| Fantastic | Rick | 01/02/01 02:59 |
| Commenting Brackets | Bruce | 01/01/01 22:58 |
| Tabs vs. Spaces | Brian Mertens | 01/01/01 21:56 |
| indent | paul | 01/01/01 21:49 |
| RE: Interesting... | Sam | 01/01/01 20:11 |
| line wrapping | Justin | 01/01/01 18:14 |
| Excellent! | Sean Grimes | 01/01/01 16:06 |
| RE: Interesting... | Jan Lehnardt | 01/01/01 11:02 |
| Indenting... | Mike | 01/01/01 01:05 |
| 2 quibbles: Indenting and brackets | Paul Meagher | 12/31/00 23:10 |
| Very nice | Arni | 12/31/00 21:20 |
| Thanks .. :) | Ryan | 12/31/00 18:15 |
| Interesting... | Chris S | 12/31/00 17:10 |
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


