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
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $name = "tushar sharma"; // wrong method echo strlen($string); //correct method if( isset($name) && is_string($name) ) { echo "Length of string is ".strlen($name)." characters"; } else { echo "Provided variable is not a string" } ?> |
Let’s check for array count & loop through the each values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $backend_technologies = ['php','python','nodejs']; // wrong way echo count($backend_technologies); foreach( $backend_technologies as $tech_name ) { echo $tech_name."<br/>"; } // coorect way if( isset($backend_technologies) && is_array($backend_technologies) ) { echo "Total number of values in array is ".count($backend_technologies)."<br/>"; echo "Array contain following values <br/>"; if( count($backend_technologies) ) { foreach( $backend_technologies as $tech_name ) { echo $tech_name."<br/>"; } } } ?> |
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.
1234//correct way of unsetting a value$variable = "some value";$variable = null;unset($variable) - 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.
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!
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.
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.
Wow, this paragraph is nice, my younger sister is analyzing
such things, therefore I am going to inform her.
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?
I m using wordpress.
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!
I am using Hueman, it’s a free wordpress theme.
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!
Thankyou for your suggestion, i will surely love to improve it with visuals and videos from my next blog.
The articles are really good! Thank you! The team is professional!!!