Tips For Effective PHP Coding In My Own Way

Tips for Effective PHP Coding in My Own Way

Don't be shellfish...Share on Facebook4Tweet about this on TwitterShare on Google+2Pin on Pinterest2Share on StumbleUpon1Share on Reddit0
Tips For Effective PHP Coding In My Own Way

Tips For Effective PHP Coding In My Own Way

Today I’m going to talk about some tips for effective PHP coding in my own way. Yes that’s right, in my own way so I hope you get some good technique or better way to code using PHP. Anyway, before we start let’s introduce first what is PHP programming language.

PHP (PHP: Hypertext Preprocessor) is one of the most well-known scripting language used to implement server side technology that designed to create an interactive webpages. Actually PHP has many competitors like ASP.NET, Ruby on Rails, Java Server Pages and others. All of this can build a server side web page but for now we are going to focus more on PHP.

Normally, the basic process of PHP is like this, the User will request a page (e.g. then the web server will process / execute the requested page using PHP compiler and will return an HTML response that will send back to the user browser.

PHP Request to Web Server

PHP Request to Web Server

So I hope you get some idea how PHP works, so let’s start on how to make your program more robust and efficient.

1.Pinpoint the Exact Problem

– This is the first thing you should do before editing or recoding your PHP program. You should do some testing; look for the process and the execution time of your code, database structure etc., and all the scenarios that you find suspicious right it down and check it with your co-developer. In that way, you could pinpoint what is the main problem that you need to solve in order to optimize your code.


2.Avoid Using Function in a Loop

– Sometimes we used this in our for loop, for example. Look the count() in your for loop.

for ($counter = 0; $counter<=count($lengthOfOurData); $counter++){
  //execute code here;

Basically, this is a common mistake for some of programmers. If you do like this, the function count () will execute every time the $counter iterate within the loop and this will cause calling the count() in several times, you will experience this slowness when you processed millions of data much better if you separate the counter to a variable.

Like this:

$countData = count($lengthOfOurData);
for ($counter = 0; $counter<=countData; $counter++){
  //execute code here;

3.Avoid SQL Queries in a For/Foreach Loop

– Probably some of the developer still do this kind of so called tradition way to insert/update all the information from the database inside a loop. (Sometimes I do, I belong here guys don’t worry. hahahaha). To avoid this, you could separate the execution of your query and build the query first in your loop before it execute. So in that case you will just request in your database once.

USING IN INSERT QUERY – Usually we do like this for inserting to our database using for/foreach loop. For this example I will used foreach loop.

foreach($userList as $insertInfo) {
$sql_query = "INSERT INTO TABLENAME (fname,lname,age) VALUES('" .$insertInfo['firstname']."', '" .$insertInfo['lastname']."', '".$insertInfo['age']."')";


Instead of the query above, you can separate your query like this.

$sql_build_data = array();

foreach($userList as $insertInfo){
  //the $sql_build_data array will contain all the sql query that we need to execute once after the loop is done.
  $sql_build_data [] = "('" . $insertInfo['firstname'] . "','" . $insertInfo['lastname'] . "')";
mysql_query("INSERT INTO TABLENAME(fname,lname,age) VALUES" . implode(",",$sql_build_data)");

4. Do you love Cache? I love Cache

– Used Memcache module in PHP because it decrease the database load time by caching the results of your database this is very effective to increase the performance of your website.

5. Used Single or Double Quotes?

– Sometimes we used single or double quotes in our php program but is there a difference ?(both are working anyway o.O).Alright to make it clear, You should used single quotes IF the string don’t have a variable inside your string like this:

$test = 'Your Awesome My Friend'; // no variable inside of this string, so used single quotes

While Double Quotes is used when you have a variable inside your string because it checks if there is a variable inside you string.

$test = "Your Awesome My Friend $getYourAzzBackThere"; // variable inside of this string, so used double quotes

So in the end, You should used them wisely. :)

6. Echo Vs Print

– Well so many of debates for this, no endless battle among the two.. Anyway i always used echo rather than print why? Because much simpler that’s it. :) No question for this.

7. SQL Wild Card

– Sometimes for us to make it type more faster we used wild card in mysql when selecting from a table like this.

$sql = "SELECT * FROM TableName";

I advice you not to used wildcard * in mysql if you don’t need all the data in that table,or much better if you type it the column field that you want to select in your database. It’s much faster that selecting them all.

8. Refactor your Code

– Basically in this part, you are going to restructure your code in your program, make it more flexible and extendable,checking if this part of code can be optimize and more readable if someone of your team handled this code,include proper comments in your code (that’s one of the major rule), and used sensible variable.


Well that’s it I hope you learn something from this post and i will try to update this post and add new tips just for you guys, If you have any suggestions or additional information on how to optimize your PHP code just, drop by a comments below and i will check it. SHARE THIS POST GUYS. Thank you.

I am Anthony Pillos 22 years old from Philippines. I'm the founder of iapdesign website. I am working as a web developer/web designer. This is my life and my passion.

Next Article20 Adobe Photoshop Tutorials Pedagogy for November