Some PHP Tips

Some PHP Tips

Some PHP Tips

There are lot of things which you should follow when working with PHP.In this article ,I am going to look into some useful tips and techniques that can be used to improve and optimize your PHP code.

These PHP tips will help you to write a more structured PHP script in less time. You should always use the following PHP hacks while coding in PHP.

1. PHP list() function

Use list() function to assign variables as an array.

Instead of:

2. array_key_exists() vs in_array()

Using array_key_exists() instead of in_array() is a good choice, because array_key_exists() is faster than in_array().

3. serialize() vs json_encode()

Use Depends on your priorities.

By default, json_encode() converts UTF-8 characters to Unicode escape sequences while serialize() does not. Note: To leave UTF-8 characters untouched, you can use the option JSON_UNESCAPED_UNICODE as of PHP 5.4.
JSON will have no memory of what the object’s original class was (they are always restored as instances of stdClass).

JSON is more portable

Try to avoid using serialize(), instead that use json_encode().

4. Ternary Operator

Ternary operator consistent with three expressions separated by a question mark (?) and a colon (:). It makes if/else logic shorter, quicker, and easier.
Use ternary operator in you script to save line and script length as long as they are simple.

5. PHP Exception Handler

6. Do not use relative paths , instead define a ROOT path

Its is common to see such lines :

This approach has many drawbacks :

It first searches for directories specified in the include paths of php , then looks from the current directory.
So many directories are checked.

When a script is included by another script in a different directory , its base directory changes to that of the including script.

Another issue , is that when a script is being run from cron , it may not have its parent directory as the working directory.

So its a good idea to have absolute paths :

Now this is an absolute path and will always stay constant. But we can improve this further. The directory /var/www/work can change , so do we change it everytime ? No instead we make it portable using magic constants like __FILE__ . Take a closer look :

So now even if you move your work to a different directory , like moving it to a live server , the same code will run without any changes.

7. Dont use require , include , require_once or include_once

Your script could be including various files on top , like class libraries , files for utility and helper functions etc like this :

This is rather primitive. The code needs to be more flexible. Write up helper functions to include things more easily. Lets take an example :

See any difference ? You must. It does not need any more explanation.
You can improve this further if you wish to like this :

There are a lot of things that can be done with this :

Search multiple directories for the same class file.
Change the directory containing class files easily , without breaking the code anywhere.
Use similar functions for loading files that contain helper functions , html content etc.

8. array_key_exists() vs in_array()

Using array_key_exists() instead of in_array() is a good choice, because array_key_exists() is faster than in_array().

9. Default Variable Value

Always assign a default value of a variable.

10. Delete the closing php tag if it is the last thing in a script

I don’t know why this trick is omitted from many blog posts on php tips and tricks.

This will save you lots of problem. Lets take an example :

A class file main_class.php

Now in index.php

And you will get Headers already send error. Why ? because the “super extra character” has been echoed , and all headers went along with that. Now you start debugging. You may have to waste many hours to find the super extra space.

Hence make it a habit to omit the closing tag :

That is better.

I hope that the above-discussed PHP tips will help you to make your script more strong.

Apart from that if you want to know more tips, please share your thoughts by providing the comment below.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *