{"id":53,"date":"2008-10-29T10:14:00","date_gmt":"2008-10-29T14:14:00","guid":{"rendered":"http:\/\/www.searchenginegenie.com\/programming-blog\/php-code-optimization-tips\/"},"modified":"2010-07-19T13:54:21","modified_gmt":"2010-07-19T17:54:21","slug":"php-code-optimization-tips","status":"publish","type":"post","link":"https:\/\/www.searchenginegenie.com\/programming-blog\/php-code-optimization-tips\/","title":{"rendered":"php code optimization tips"},"content":{"rendered":"<p>Do you know the ways to optimize your php code. Here we have some tips for you.<\/p>\n<ul>\n<li>If a method can be static, declare it as static. It will improve the Speed by a factor of 4.<\/li>\n<li>You should use echo as it is faster than print.<\/li>\n<li>If you have to concat string, Use echo&#8217;s multiple parameters.<\/li>\n<li>Set the value of how many times it should be iterated before the loop. Do not assign the value in the loop.<\/li>\n<li>When a variable is no longer useful, unset those variables which will free memory.<\/li>\n<li>Use absolute urls in includes and requires.<\/li>\n<li>To get the time of when the script started its execution, you must prefer $_SERVER [&#8216;REQUEST_TIME&#8217;] than time ()<\/li>\n<li>Str_replace is faster than preg_replace<\/li>\n<li>It is much better to utilize switch statements than multi if, else if statements.<\/li>\n<li>Every time close your database connections when you got all the datas from DB<\/li>\n<li>Always use $row [&#8216;name&#8217;] to get data from DB.Because it is 7 times faster than $row [name]<\/li>\n<li>Don&#8217;t use functions inside of for loop, such as for ($y=1; $y &lt;= main ($arr); $y) the main () function gets called each time.<\/li>\n<li>If you declare a global variable and you are not using it in any function will slow down the performance because will check if the global exists or not.<\/li>\n<li>Methods should be in derived classes. This will run faster than the method which was defined in the base class.<\/li>\n<li>Surrounding your string by single quote (&#8216;) instead of double quotes (&#8220;) will be little faster as php looks for variables inside &#8220;&#8221; but not inside &#8221;.<\/li>\n<li>A PHP script will be at least 2-10 times slower than a static HTML page by Apache. So it is better to use lot of static HTML pages with less scripts.<\/li>\n<li>You must install a PHP caching product to naturally boost the performance by 25-100% by eliminating compile times. Your PHP scripts will be recompiled each time if the scripts are not cached.<\/li>\n<li>Do not use OOP concepts too much because each method and object call consumes a lot of memory.<\/li>\n<li>Split methods that has the code you will really re-use too much.Do not split all the functions.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Do you know the ways to optimize your php code. Here we have some tips for you. If a method can be static, declare it as static. It will improve the Speed by a factor of 4. You should use echo as it is faster than print. If you have to concat string, Use echo&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[63],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-tips","tag-php-code-optimization"],"_links":{"self":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":2,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/53\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}