Tips & Tricks for writing better PHP code


PHP is one of the widely used Server Side Scripting Language. It’s easy to learn PHP but it’s tricky to use it correctly. Let’s see some of the tips & tricks for writing better PHP code.

Variable Validation
Consider a scenario where we have to find array count or string length of a variable and we have to handle all kinds of errors and warnings.This sounds very simple but most of the newbies can’t complete it (with my experience) because they don’t know how to write quality code.
Let’s calculate length of a string

Let’s check for array count & loop through the each values

Return type of inbuilt function
PHP comes with so much of inbuilt funtions and of course it’s important to use them but always remember their “return type”. Let’s understand this with an interview of fresher which took some days ago.
Me: Can u tell me return type of date(“Y-m-d”) ?
Fresher: String
Me: Can u tell me return type of date(“YW”) ?
Fresher: Integer
And that’s the problem, for newbies return type means what they see as output, in this case if fresher saw 2016-01-02 then return type of date was string and if it was 201601 the it is integer, which is wrong.Return type of a function never varies with the output.

Free memory
Memory is a very important aspect of any application. PHP is not so good with it’s memory management by itself but we can take care of it with some simple tips as follows:

  • Unset the values when after it’s use.
  • while running an infinite loop (like in background services) always use gc_collect_cycles, this will collect any garbage cycles, thus reducing memory

Limit memory
It’s always good practice to limit memory of a php script.Say we have to set memory limit to 100Mb then we should write is as ini_set(‘memory_limit’, ‘100M’);

Follow MVC approach
Models deals with database(s), Controller contains logic and Views contains front-end design.It is the best approach to maintain your code and separate logic with design. MVC provides a single file to handle your incoming request, which makes it very easily to handle, debug and maintain requests

Use arrays
I prefer arrays over a simple variable just because it’s easy to maintain and perform operations on array than a single variable. But don’t overuse it, just use it wisely.

I hope these tips and tricks will help you to write more robust code.

11 thoughts on “Tips & Tricks for writing better PHP code”

  1. This is the right website for anybody who wants to understand this topic.
    You realize a whole lot its almost hard to argue with you (not that I really
    will need to…HaHa). You definitely put a new spin on a topic which has been discussed for decades.
    Wonderful stuff, just excellent!

  2. Hello everyone, it’s my first pay a visit at this website,
    and article is in fact fruitful in favor of me, keep up posting these content.

  3. You can definitely see your enthusiasm within the work
    you write. The arena hopes for even more passionate writers such as you who
    are not afraid to say how they believe. All the time follow your heart.

  4. Right now it sounds like Drupal is the best blogging platform out there right now.
    (from what I’ve read) Is that what you’re using
    on your blog?

  5. I truly love your website.. Pleasant colors & theme.
    Did you create this amazing site yourself? Please reply back
    as I’m planning to create my very own site and would like to learn where you got this from
    or exactly what the theme is called. Cheers!

  6. Have you ever thought about including a little bit more
    than just your articles? I mean, what you say is important and everything.

    However think of if you added some great visuals or videos to give your posts more, “pop”!
    Your content is excellent but with images and clips, this
    website could certainly be one of the most
    beneficial in its field. Awesome blog!

Leave a Reply