redneonrt Posted September 6, 2006 Share Posted September 6, 2006 I was looking for something to show me how to write and format PHP code correctly, things like when to tab in and proper spacing ect.Thanks for looking Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/ Share on other sites More sharing options...
roopurt18 Posted September 6, 2006 Share Posted September 6, 2006 I set my editor to replace tab keystrokes with two spaces.The simple rule I use is to press tab on a new line after any opening curly brace and to delete two spaces when I type the closing curly brace. Always use curly braces, even when they're optional. And never let a line go past 80 characters. Use lots of comments, avoiding the block comment (/* */) syntax so you can comment out blocks later if need be.[code]// Define constsdefine('CONST1', 'VAL01');define('CONST2', 'VAL02');// This class does blah blah yadda yadda// Use for asdf qwertyclass MyClass extends MyBase{ function MyClass(){ // comment $var = $val; if(isset($something)){ // comment // comment $hello = $world; } $this->ReallyLongVarName = SomeOtherClass::ReallyLongFuncName( $val01, $val02, $val03 ); $this->arr = Array( $ReallyLongArrayValue01, AnotherBetterClass::WithAnotherLongFunc(), ); return; }}[/code]$val02 and $val03 in the function call within SomeOtherClass should be directly below $val01, my browser isn't placing it correctly when I view my post though.Last note, there is no right or wrong way. But there are best and worst ways. The best ways are so that you can come back to the same code written a few months ago, or not even your code, and still figure out what it does. Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87250 Share on other sites More sharing options...
wildteen88 Posted September 6, 2006 Share Posted September 6, 2006 Yeah I agree with roopurt18, however I prefer 4 spaces rather than two for a tab, and i like my curly braces to be on thier own line, eg:[code=php:0]// Define constsdefine('CONST1', 'VAL01');define('CONST2', 'VAL02');// This class does blah blah yadda yadda// Use for asdf qwertyclass MyClass extends MyBase { function MyClass() { // comment $var = $val; if(isset($something)) { // comment // comment $hello = $world; } return true; }}[/code]IMO it makes the code more readable, and you can tell where your code blocks start and end, where as if the opening curly brace isn't on its own you can only see where the code block ends. It also makes it easier to pair up the curly braces too. Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87255 Share on other sites More sharing options...
redneonrt Posted September 6, 2006 Author Share Posted September 6, 2006 So its really up to the person writing it? Whatever makes it easiest for them to read? or are there a set guidline to follow? Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87259 Share on other sites More sharing options...
wildteen88 Posted September 6, 2006 Share Posted September 6, 2006 There is no set guidelines to how PHP code should be formated AFAIK. Its mainly down to the personal preference of the programmer. However if developing a script with a group of other programmers and they all have a different styles of coding, then there should be guidelines set out for developers to follow when programming the web application, to help keep the code clear and readable.As long as the code is clear and readable to anyone that sees it then it doesnt matter what style you format your code. Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87272 Share on other sites More sharing options...
redneonrt Posted September 6, 2006 Author Share Posted September 6, 2006 OK thanks for everyones response Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87275 Share on other sites More sharing options...
Daniel0 Posted September 6, 2006 Share Posted September 6, 2006 I agree with wildteen. Additionally I prefer to write [tt]else if[/tt] instead of [tt]elseif[/tt] like some people do. And instead of [code]echo "Your name is ".$name."!";[/code] I prefer to use [code]echo "Your name is {$name}!";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87277 Share on other sites More sharing options...
.josh Posted September 6, 2006 Share Posted September 6, 2006 many different "styles." the key is consistency throughout the code. And don't be afraid to use comments. Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87280 Share on other sites More sharing options...
Daniel0 Posted September 6, 2006 Share Posted September 6, 2006 [quote author=Crayon Violent link=topic=107129.msg429390#msg429390 date=1157564770]And don't be afraid to use comments. [/quote]Don't comment the obvious, however. E.g.: [code]echo $username; // echo'es the username[/code] or [code]if(is_array($var)) // check if $var is an array[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87292 Share on other sites More sharing options...
Barand Posted September 6, 2006 Share Posted September 6, 2006 A guideline for commentsAsk yourself "If I come back to this piece of code in six months time, or someone else looks at it, will we immediately understand what it's doing and how it works?"If the answer is "No", add comments Quote Link to comment https://forums.phpfreaks.com/topic/19923-php-formatting/#findComment-87349 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.