{"id":1526,"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-4\/"},"modified":"2018-01-19T19:24:51","modified_gmt":"2018-01-19T19:24:51","slug":"best-practices-php-coding-style-page-4","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/best-practices-php-coding-style-page-4\/","title":{"rendered":"Best Practices: PHP Coding Style Page 4"},"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>PHP Tags<\/h2>\n<div class=\"articlePara\">\nYou should always use the full &lt;?php ?&gt; tags to enclose your code, not the<br \/>\nabbreviated &lt;? ?&gt; tags, which could conflict with XML or other languages.<br \/>\nASP-style tags, while supported, are messy and discouraged.<\/div>\n<h2>Strings<\/h2>\n<div class=\"articlePara\">\nThe proper use of strings is not really discussed in the PEAR RFC, but I&#8217;m going to<br \/>\ncover it anyway. In PHP of course, double quotes (&#8220;) are parsed, but single quotes (&#8216;)<br \/>\nare not. That means PHP does extra work (magic really) on double-quoted strings that it doesn&#8217;t<br \/>\ndo on single-quoted strings. So there is a (subtle) performance difference between the two,<br \/>\nespecially in code that is iterated or called a large number of times.<\/div>\n<div class=\"articleHeader2\">Best:<\/div>\n<div class=\"example\">\n<pre>\n$associative_array['name'];\n$var='My String';\n$var2='Very... long... string... ' . $var . ' ...more string... ';\n$sql=\"INSERT INTO mytable (field) VALUES ('$var')\";\n<\/pre>\n<\/div>\n<div class=\"articleHeader2\">Acceptable:<\/div>\n<div class=\"example\">\n<pre>\n$associative_array[\"name_$x\"];\n$var=\"My String $a\";\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nThe first example does not include any vars that need to be appended or parsed into the strings,<br \/>\nso single quotes (&#8216;) are definitely the best way to go. The second example includes very short<br \/>\nstrings and has variables that need to be parsed into the strings, so it is acceptable to use<br \/>\ndouble quotes. If the strings were long, it would be best to use the append (.) operator.<\/div>\n<div class=\"articlePara\">\nFor consistency&#8217;s sake, I always use single quotes around all my strings, except SQL statements,<br \/>\nwhich almost always contain apostrophes and variables that must be parsed into the strings.<\/div>\n<div class=\"articlePara\">\nWell, now you&#8217;ve got the fundamentals of good coding style to work with. If you have<br \/>\ncomments, please post them below. Not everyone will agree with the standards laid out here,<br \/>\nnor should they. I may respectfully disagree with certain aspects of this RFC, but I<br \/>\nstrongly agree with the core value of it and its goals (to improve PHP coding for everyone).<\/div>\n<div class=\"articlePara\">\n&#8211;Tim\n<\/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=\"tim200101019ba9.html?page=3\">\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=\"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=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">4<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Tim Perdue on January 3, 2001 PHP Tags You should always use the full &lt;?php ?&gt; tags to enclose your code, not the abbreviated &lt;? ?&gt; tags, which could conflict with XML or other languages. ASP-style tags, while supported, are messy and discouraged. Strings The proper use of strings&#8230; <a href=\"https:\/\/phpbuilder.com\/best-practices-php-coding-style-page-4\/\" 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-1526","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1526","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=1526"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1526\/revisions"}],"predecessor-version":[{"id":3359,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1526\/revisions\/3359"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}