{"id":1525,"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-3\/"},"modified":"2018-01-19T19:24:51","modified_gmt":"2018-01-19T19:24:51","slug":"best-practices-php-coding-style-page-3","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/best-practices-php-coding-style-page-3\/","title":{"rendered":"Best Practices: PHP Coding Style Page 3"},"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>Function Calls<\/h2>\n<div class=\"articlePara\">\nFunctions should be called with no space between between the function name and<br \/>\nthe parentheses. Spaces between params and operators are encouraged.<\/div>\n<div class=\"example\">\n<pre>\n$var = myFunction($x, $y);\n<\/pre>\n<\/div>\n<h2>Functions<\/h2>\n<div class=\"articlePara\">\nFunction calls are braced differently than conditional expressions. This is a<br \/>\ncase where I&#8217;ll have to change my personal coding style in order to be kosher,<br \/>\nbut I guess it needs to be done.<\/div>\n<div class=\"example\">\n<pre>\nfunction myFunction($var1, $var2 = '')\n{\n\t\/\/indent all code inside here\n\treturn $result;\n}   \n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nNotice again there is no space between the function name and the parens, and that<br \/>\nthe params are nicely spaced. All your code inside the function will be at least<br \/>\n4 spaces indented.<\/div>\n<div class=\"articlePara\">\nAnother important principle when coding functions is that they should always<br \/>\n<b>return instead of print directly<\/b>. Remember, if you print directly inside<br \/>\nyour function call, whomever calls your function cannot capture its output using a<br \/>\nvariable.<\/div>\n<h2>Comments<\/h2>\n<div class=\"articlePara\">\nBorrowing &#8211; almost in its entirety &#8211; from the JavaDoc spec, PEAR strongly encourages<br \/>\nthe use of <a href=\"http:\/\/www.phpdoc.de\/\" target=\"_blank\">PHPDoc<\/a> comment style. JavaDoc is rather<br \/>\nclever because you format your<br \/>\ncode comments in such a manner that they can be parsed by a doc-generating tool,<br \/>\nessentially creating &#8220;self commenting&#8221; code. You can then view the resulting docs<br \/>\nusing a web browser.<\/div>\n<div class=\"articlePara\">\nAs a reformed java programmer myself, I really like this standard and am trying to<br \/>\ngo back through my code and add PHPDoc comments.<\/div>\n<div class=\"example\">\n<pre>\n\/**\n *\tshort description of function\n *\n *\tOptional more detailed description.\n *\n *\t@param $paramName - type - brief purpose\n *\t@param ...\n *\t...\n *\t@return type and description\n *\/\n<\/pre>\n<\/div>\n<h2>Including Code<\/h2>\n<div class=\"articlePara\">\nPHP4 introduced a pretty useful new feature: include_once(). I&#8217;ve seen a lot<br \/>\nof questions on the mailing lists and discussion boards caused by people including<br \/>\nthe same files in multiple places. This can cause conflicts when the included files<br \/>\ninclude functions, which can be defined only once per script.<\/div>\n<div class=\"articlePara\">\nThe simple solution is to replace all your include() and require() calls with<br \/>\ncorresponding include_once() and require_once(). Both use the same file list, so a<br \/>\nfile included with require_once() will not be later included with an include_once()<br \/>\ncall. (technically, require_once and include_once are &#8220;operators&#8221;, not &#8220;functions&#8221;<br \/>\nso parens are not necessary).<\/div>\n<div class=\"articlePara\">\nI never use include, or any of these *_once functions. IMHO, well-designed applications<br \/>\ninclude files in one place that is easy to maintain and keep track of. Others will disagree<br \/>\nand say that each included file should require_once() every file that it depends on.<br \/>\nThis seems like extra overhead and maintenance headaches to me personally.<br \/>\nEither way, the days of conflicting includes are probably over.<\/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=\"tim200101014658.html?page=2\">\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=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tim200101014658.html?page=2\">2<\/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;\">3<\/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=\"tim20010101fdb0.html?page=4\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Tim Perdue on January 3, 2001 Function Calls Functions should be called with no space between between the function name and the parentheses. Spaces between params and operators are encouraged. $var = myFunction($x, $y); Functions Function calls are braced differently than conditional expressions. This is a case where I&#8217;ll&#8230; <a href=\"https:\/\/phpbuilder.com\/best-practices-php-coding-style-page-3\/\" 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-1525","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1525","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=1525"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1525\/revisions"}],"predecessor-version":[{"id":3358,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1525\/revisions\/3358"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}