{"id":1524,"date":"2018-01-19T19:22:56","date_gmt":"2018-01-19T19:22:56","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/best-practices-php-coding-style-page-2\/"},"modified":"2018-01-19T19:24:51","modified_gmt":"2018-01-19T19:24:51","slug":"best-practices-php-coding-style-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/best-practices-php-coding-style-page-2\/","title":{"rendered":"Best Practices: PHP Coding Style Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Tim Perdue<\/div>\n<div class=\"\">on January 3, 2001<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>Indenting<\/h2>\n<div class=\"articlePara\">\nAll your code should be properly indented. This is the most fundamental<br \/>\nthing you can do to improve readability. Even if you don&#8217;t comment your<br \/>\ncode, indenting will be a big help to anyone who has to read your code<br \/>\nafter you.<\/div>\n<div class=\"example\">\n<pre>\nwhile ($x &lt; $z) {\n\tif ($a == 1) {\n\t\techo 'A was equal to 1';\n\t} else {\n\t\tif ($b == 2) {\n\t\t\t\/\/do something\n\t\t} else {\n\t\t\t\/\/do something else\n\t\t}\n\t}\n}\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nThe PEAR RFC standard calls for 4 spaces, not tabs of any size, in your code.<br \/>\nI disagree with this personally and will continue to tab my code. My view is<br \/>\nthat tabs rather than spaces will create smaller files and smaller files are<br \/>\nfaster to parse, upload, download, etc etc. The other advantage to using tabs<br \/>\nis that you can set your tab size to your personal preference when viewing<br \/>\nsomeone else&#8217;s code. I used to use 8-space tabs, but recently switched to<br \/>\n4-space tabs and all my code &#8220;reformatted&#8221; automatically by just setting a<br \/>\npreference in vim.<\/div>\n<h2>Control Structures<\/h2>\n<div class=\"articlePara\">\nThis is pretty much common sense, but I still see an awful lot of code<br \/>\nthat is not braced for readability. If you use conditional expressions (IF statements)<br \/>\nwithout braces, not only is it less readable, but bugs can also be introduced when<br \/>\nsomeone modifies your code.<\/div>\n<div class=\"articleHeader2\">Bad Example:<\/div>\n<div class=\"example\">\n<pre>\nif ($a == 1) echo 'A was equal to 1';\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nThat&#8217;s pretty much illegible. It may work for you, but the person following<br \/>\nafter you won&#8217;t appreciate it at all.<\/div>\n<div class=\"articleHeader2\">Less Bad Example:<\/div>\n<div class=\"example\">\n<pre>\nif ($a == 1) \n\techo 'A was equal to 1';\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nNow that&#8217;s at least readable, but it&#8217;s still not maintainable. What if I want<br \/>\nan additional action to occur when $a==1? I need to add braces, and if I forget<br \/>\nto, I&#8217;ll have a bug in my code.<\/div>\n<div class=\"articleHeader2\">Correct:<\/div>\n<div class=\"example\">\n<pre>\nif (($a == 1) &amp;&amp; ($b==2)) {\n\techo 'A was equal to 1';\n\t\/\/easily add more code\n} elseif (($a == 1) &amp;&amp; ($b==3)) {\n\t\/\/do something\n}\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nNotice the space after the <b>if<\/b> and <b>elseif<\/b> &#8211; this helps distinguish<br \/>\nconditionals from function calls.<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"tim20010101.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim20010101.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim200101019ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim20010101fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"tim200101019ba9.html?page=3\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Tim Perdue on January 3, 2001 Indenting All your code should be properly indented. This is the most fundamental thing you can do to improve readability. Even if you don&#8217;t comment your code, indenting will be a big help to anyone who has to read your code after you&#8230;. <a href=\"https:\/\/phpbuilder.com\/best-practices-php-coding-style-page-2\/\" class=\"readmore\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1524","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1524"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1524\/revisions"}],"predecessor-version":[{"id":3357,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1524\/revisions\/3357"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}