{"id":544,"date":"2014-06-03T12:32:49","date_gmt":"2014-06-03T12:32:49","guid":{"rendered":"http:\/\/www.siriinnovations.com\/blog\/?p=544"},"modified":"2014-07-10T11:43:30","modified_gmt":"2014-07-10T11:43:30","slug":"tips-optimizing-php-code","status":"publish","type":"post","link":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/","title":{"rendered":"Tips for Optimizing PHP code"},"content":{"rendered":"<ol>\n<li>If a method can be static, declare it <strong>static<\/strong>, <strong>speed<\/strong> can be improved.<\/li>\n<li><strong>Echo<\/strong> is more faster than print.<\/li>\n<li>Use <strong>Echo&#8217;s multiple<\/strong> parameters <strong>instead of string concatenation<\/strong>.<\/li>\n<li><strong>Unset<\/strong> your variables to free memory, especially large arrays.<\/li>\n<li>Avoid magic like <strong>__get, __set, __autoload.<\/strong><\/li>\n<li>Require_once() is <strong>expensive<\/strong>.<\/li>\n<li>Use full paths in includes and requires, less time spent on resolving the OS paths.<\/li>\n<li><strong>Str_replace<\/strong> is faster than preg_replace, but strtr is faster than str_replace<\/li>\n<li><strong>Error suppression<\/strong> with @ is very slow.<\/li>\n<li>Close your database connections when you&#8217;re done with them<\/li>\n<li>Error messages are expensive<\/li>\n<li>Incrementing a global variable is 2 times slower than a local var.<\/li>\n<li>Incrementing an object property (eg. $this-&gt;prop++) is 3 times slower than a local variable.<\/li>\n<li>Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.<\/li>\n<li>Use any frame works (For example cake php,yii frame work)<\/li>\n<li>Use <strong>Ternary Operators<\/strong> in your coding<\/li>\n<li>Methods in derived classes run faster than ones defined in the base class.<\/li>\n<li>Use <strong>if else conditions<\/strong> are more faster than the<strong> select statements<\/strong>.<\/li>\n<li><strong>Avoid looping<\/strong> in your coding.When you use array go for the foreach loop rather than for loop.<\/li>\n<li>Don\u2019t use<strong> short tags<\/strong> in your coding.<\/li>\n<li>++$i is more faster than $ i++ ,use pre-increment when it is possible.<\/li>\n<li>Avoid the PHP mail() function header injection issue.<\/li>\n<li>$row[\u2019id\u2019] is 7 times faster than $row[id], because if you don\u2019t supply quotes it has to guess which index you meant, assuming you didn\u2019t mean a constant.<\/li>\n<li>Separate code, content and presentation: keep your PHP code separate from your HTML.<\/li>\n<li>Avoid using plain text when storing and evaluating passwords to avoid exposure, instead use a hash, such as an md5, hash.<\/li>\n<li>Not everything has to be OOP, often it is just overhead, each method and object call consumes a lot of memory.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>If a method can be static, declare it static, speed can be improved. Echo is more faster than print. Use Echo&#8217;s multiple parameters instead of string concatenation. Unset your variables to free memory, especially large arrays. Avoid magic like __get, __set, __autoload. Require_once() is expensive. Use full paths in includes and requires, less time spent [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":1028,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,3,15],"tags":[],"class_list":["post-544","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tutorials","category-technical","category-tips"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tips for Optimizing PHP code - Siri Innovations<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tips for Optimizing PHP code - Siri Innovations\" \/>\n<meta property=\"og:description\" content=\"If a method can be static, declare it static, speed can be improved. Echo is more faster than print. Use Echo&#8217;s multiple parameters instead of string concatenation. Unset your variables to free memory, especially large arrays. Avoid magic like __get, __set, __autoload. Require_once() is expensive. Use full paths in includes and requires, less time spent [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Siri Innovations\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-03T12:32:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-07-10T11:43:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"540\" \/>\n\t<meta property=\"og:image:height\" content=\"241\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sindhuja Buddha\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sindhuja Buddha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/\"},\"author\":{\"name\":\"Sindhuja Buddha\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/ac86eb950dbc271e45bc61ebdc709cb3\"},\"headline\":\"Tips for Optimizing PHP code\",\"datePublished\":\"2014-06-03T12:32:49+00:00\",\"dateModified\":\"2014-07-10T11:43:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/\"},\"wordCount\":312,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg\",\"articleSection\":[\"PHP Tutorials\",\"Technical\",\"Tips\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/\",\"url\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/\",\"name\":\"Tips for Optimizing PHP code - Siri Innovations\",\"isPartOf\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg\",\"datePublished\":\"2014-06-03T12:32:49+00:00\",\"dateModified\":\"2014-07-10T11:43:30+00:00\",\"author\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/ac86eb950dbc271e45bc61ebdc709cb3\"},\"breadcrumb\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage\",\"url\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg\",\"contentUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg\",\"width\":540,\"height\":241},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/siriinnovations.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tips for Optimizing PHP code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#website\",\"url\":\"https:\/\/siriinnovations.com\/blog\/\",\"name\":\"Technical blog from Siri Innovations\",\"description\":\"Innovative like no other\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/siriinnovations.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/ac86eb950dbc271e45bc61ebdc709cb3\",\"name\":\"Sindhuja Buddha\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d06cf5ad8a80f503b244a6bf5f560ba1308825423171fc61c55cf1267b5df2bd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d06cf5ad8a80f503b244a6bf5f560ba1308825423171fc61c55cf1267b5df2bd?s=96&d=mm&r=g\",\"caption\":\"Sindhuja Buddha\"},\"url\":\"https:\/\/siriinnovations.com\/blog\/author\/sindhuja-b\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tips for Optimizing PHP code - Siri Innovations","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/","og_locale":"en_US","og_type":"article","og_title":"Tips for Optimizing PHP code - Siri Innovations","og_description":"If a method can be static, declare it static, speed can be improved. Echo is more faster than print. Use Echo&#8217;s multiple parameters instead of string concatenation. Unset your variables to free memory, especially large arrays. Avoid magic like __get, __set, __autoload. Require_once() is expensive. Use full paths in includes and requires, less time spent [&hellip;]","og_url":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/","og_site_name":"Siri Innovations","article_published_time":"2014-06-03T12:32:49+00:00","article_modified_time":"2014-07-10T11:43:30+00:00","og_image":[{"width":540,"height":241,"url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg","type":"image\/jpeg"}],"author":"Sindhuja Buddha","twitter_misc":{"Written by":"Sindhuja Buddha","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#article","isPartOf":{"@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/"},"author":{"name":"Sindhuja Buddha","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/ac86eb950dbc271e45bc61ebdc709cb3"},"headline":"Tips for Optimizing PHP code","datePublished":"2014-06-03T12:32:49+00:00","dateModified":"2014-07-10T11:43:30+00:00","mainEntityOfPage":{"@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/"},"wordCount":312,"commentCount":0,"image":{"@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage"},"thumbnailUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg","articleSection":["PHP Tutorials","Technical","Tips"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/","url":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/","name":"Tips for Optimizing PHP code - Siri Innovations","isPartOf":{"@id":"https:\/\/siriinnovations.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage"},"image":{"@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage"},"thumbnailUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg","datePublished":"2014-06-03T12:32:49+00:00","dateModified":"2014-07-10T11:43:30+00:00","author":{"@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/ac86eb950dbc271e45bc61ebdc709cb3"},"breadcrumb":{"@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#primaryimage","url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg","contentUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg","width":540,"height":241},{"@type":"BreadcrumbList","@id":"https:\/\/siriinnovations.com\/blog\/tips-optimizing-php-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/siriinnovations.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Tips for Optimizing PHP code"}]},{"@type":"WebSite","@id":"https:\/\/siriinnovations.com\/blog\/#website","url":"https:\/\/siriinnovations.com\/blog\/","name":"Technical blog from Siri Innovations","description":"Innovative like no other","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/siriinnovations.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/ac86eb950dbc271e45bc61ebdc709cb3","name":"Sindhuja Buddha","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d06cf5ad8a80f503b244a6bf5f560ba1308825423171fc61c55cf1267b5df2bd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d06cf5ad8a80f503b244a6bf5f560ba1308825423171fc61c55cf1267b5df2bd?s=96&d=mm&r=g","caption":"Sindhuja Buddha"},"url":"https:\/\/siriinnovations.com\/blog\/author\/sindhuja-b\/"}]}},"jetpack_featured_media_url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/06\/23.jpg","_links":{"self":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/comments?post=544"}],"version-history":[{"count":4,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":776,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/544\/revisions\/776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/media\/1028"}],"wp:attachment":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}