Useful PHP Tips for Beginners – Part 2

11. Do not gzip output in your application , make apache do that

Thinking of using ob_gzhandler ? No dont do that. It doesnt make sense. Php is supposed to write your application. Dont worry about how to optimise data transfer between server and browser inside Php.

Use apache mod_gzip/mod_deflate to compress content via the .htaccess file.

12. Use json_encode when echoing javascript code from php

There are times when some javascript code is generated dynamically from php.

Be smart. use json_encode :

Isn’t that neat ?

13. Check if directory is writable before writing any files

Before writing or saving any file , make sure you check that the directory is writable or not , and flash an error message if it is not. This will save you a lot of “debugging” time. When you are working on a linux , permissions have to be dealt with and there would be many many permission issues when directories would not be writable , files would not be readable and so on.

Make sure that your application is as intelligent as possible and reports the most important information in the shortest time.

That is totally correct. But has some indirect problems. The file_put_contents may fail for a number of reasons :

  • Parent directory does not exist
  • Directory exists , but is not writable
  • File locked for writing ?

So its better to make everything clear before writing out to a file.

By doing this you get the accurate information that where is a file write failing and why

14. Change permission of files that your application creates

When working in linux environment , permission handling can waste a lot of your time. Hence whenever your php application creates some files do a chmod over them to ensure they are “accessible” outside. Otherwise for example the files may be created by “php” user and you are working as a different user and the system wont let you access or open the file , and then you have to struggle to get root privileges , change the permissions of the file and so on.

15. Don’t check submit button value to check form submission

The above is mostly correct , except when your application is multi-lingual. Then the ‘Save’ can be many different things. How would you compare then. So do not rely on the value of submit button. Instead use this :

Now you are free from the value the submit button

16. Use static variables in function where they always have same value

Instead use static variables as :

17. Don’t use the $_SESSION variable directly

Some simple examples are :

But this has a problem. If you are running multiple applications on the same domain , the session variables my conflict. 2 different applications may set the same key name in the session variable. Take for example , a frontend portal , and the backend management application , on the same domain.

Hence use application specific keys with wrapper functions :

18. Wrap utility helper functions into a class

So you have a lot of utility functions in a file like :

And you use the function throughout your application freely. You may want to wrap them into a class as static functions :

One clear benefit you get here is if php has inbuilt functions with similar names , then names will not conflict.
Another perspective , though little advanced is that you can maintain multiple versions of the same class in the same application without any conflict. Its basically encapsulation , nothing else.

19. Bunch of silly tips

  • Use echo instead of print
  • Use str_replace instead of preg_replace , unless you need it absolutely
  • Do not use short tags
  • Use single quotes instead of double quotes for simple strings
  • Always remember to do an exit after a header redirect
  • Never put a function call in a for loop control line.
  • isset is faster than strlen
  • Format your code correctly and consistently
  • Do not drop the brackets of loops or if-else blocks.
    Do not code like this :

if ( $a == true) $a_count++;

its absolutely a WASTE.


if( $a == true) { $a_count++; }

Don’t try to make your code shorter by eating up syntax. Rather make your logic shorter.

  • Use a proper text editor which has code highlighting. Code highlighting helps to create lesser errors.

20. Process arrays quickly with array_map

Lets say you want to trim all elements of an array. Newbies do it like this :

But it can more cleaner with array_map :

$arr = array_map('trim', $arr);

This will apply trim on all elements of the array $arr. Another similar function is array_walk. Check out the
documentation on these to know more.

Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.