May 05, 2010

Cool PHP Tricks for Smart Coders

PHP is an inventive language which lets you experiment with various forms of coding. You can try implementing some cool tricks to simplify and shorten your code. Let us see some:

Use echo like a function: It is normally thought that with echo you can concatenate (join) strings using period. However, you can implement echo as a function and use comma instead of periods. It is also faster. Here is a code snippet:

Make maximum use of single quotes: It is a good programming practice to use single quotes when possible in PHP as it saves time. PHP does not need to parse strings for variables if you use single quotes. Another important tip is that while referencing an array that has a string index, use single quotes.

Make use of arrays in form fields: You can create a form field and create an element in an array. You can also create dynamic arrays in a form field to implement check boxes where the user can check multiple boxes.

Buffering the output: You may happen to get some output on the console, when you do not want. Particularly if you re-use a framework or a template which has a display instruction, you might want to prevent anything from being displayed at that point of time. Here you can use the concept of output buffering. Here is a code snippet:

You can definitely figure out many more such tricks to make your code look smart and execute faster.

Hide Sidebar →

Subscribe to Our Newsletters

Enterprise Content Management

Most Read Posts


Hide Sidebar →