PHP Coding Guidelines – A cheat sheet

March 13th, 2010

A cheat sheet, listing some guidelines for good coding practices.

Indenting
- Use tabs for indenting.
- Set tab to 4 spaces.
- Keep lines <80 chars long, for better readability.

Spacing and Linefeeds

- No trailing whitespace at the end of the lines.
- Use Unix style formatting.
* Lines must end only with a line feed (LF).
* All text files should end in a single newline (\n) after the closing PHP tag (?>).
- If you are using Windows platform for development, please set your editor to save files in Unix format.

Naming Conventions

- Use the following naming conventions.
* filenames-in-general
* javascriptFunctions
* javascriptVariables
* mysql_columns
* mysql_tables
* _php_global_vars
* php_functions
* php_variables
* CONSTANT_NAMES
* phpClassMethods
* phpClassNames
* README.txt

Arrays

- Arrays should be formatted with a space separating each element (after the comma)

$array_number_one = array('this', 'is', 'foo' => 'bar');

- If the line declaring an array spans longer than 80 characters, each element should be broken into its own line with proper indentation.
$form['title']=array(
'#type'=>'textfield',
'#title'=>'Post Title',
'#maxlength' => 32,
'#required' => TRUE,
'#size' => 15,
);

Function Arguments

- Function arguments should be separated by spaces, both in the definitions and in function calls. There should not be any spaces between the arguments and the opening and closing brackets, or between the function name and the opening bracket.

Eg: function this_is_a_test($argument1, $argument2)

- Arguments with default values should always go at the end of the argument list.
Eg: function this_is_a_test($argument1, $argument2, $argument3 = 'abc')

PHP Code Layout

- Do NOT combine multiple lines of code into a single line, even if they are comments or debug statements.

Eg:

$variable = $something + $something; $another_variable = $something_else; echo "test"; //WRONG

Correct way:

$variable = $something + $something;

$another_variable = $something_else;

echo "test";

- Braces should ALWAYS be included when writing code using if, for, while etc. blocks. No exceptions here, even if the braces could be omitted. Leaving out braces makes code harder to maintain in the future and can also cause bugs that are very difficult to track down.

Eg:
Wrong practices:
if ($condition1) echo "some code";
if ($condition1)
echo "some code";
else
echo "some code";

Correct way:
if ((condition1) || (condition2)) {
//action1
} elseif ((condition3) && (condition4)) {
//action2
} else {
//default action
}

- Control statements should have one space between the control keyword and opening parenthesis.

- Split lengthy IF statements into several lines.
if (($condition1
&& $condition2)
|| $condition3
|| $condition4
) {
//do something
}

SQL Code Layout

- Do NOT hard code db_prefix in table names. (Specific to Drupal).
- Always capitialise all SQL keywords (SELECT, FROM, VALUES, AS etc.) and leave everything else in the relevant case.
- If you are using WHERE clauses to return data corresponding to a set of conditions, enclose those conditions in brackets in the same way you would for PHP if blocks.

Eg: SELECT * FROM {node} WHERE ( (rid = 5) AND ((type = 'recording') OR (blah = 'somevalue')) )

- Split lengthy SQL statements into several lines.

Quoted Strings

- Strings in PHP can either be quoted with single quotes (”) or double quotes (”"). The difference between the two is that the parser will use variable-interpolation in double-quoted strings, but not with single-quoted strings. So if your string contains no variables, use single quotes, otherwise, use double quotes.

SQL Injection

- Make sure your SQL code does not expose SQL Injection vulnerability.

Eg: This code is prone to SQL Injection.
db_query("INSERT into {node} (nid, vid, type, uid, created, changed) VALUES($nid, $vid, '" . $type . "', $user->uid, $created, $changed)");

Instead, use:
db_query("INSERT into {node} (nid, vid, type, uid, created, changed) VALUES(%d, %d, '%s', %d, %d, %d)",$nid, $vid, $type , $user->uid, $created, $changed);

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

Tech , , , ,

Protected: 2009 – the year that was

November 18th, 2009
Enter your password to view comments

This post is password protected. To view it please enter your password below:


Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

Tech

How To – monitor system performance on Ubuntu with sysstat

May 11th, 2009

1. Install SysStat
apt-get install sysstat

2. Enable Stat collection
Open vi /etc/default/sysstat
Edit the file, change “ENABLED” to “true”.

3. Start Sysstat
/etc/init.d/sysstat start

3.a. By default, sysstat reports activity every 10 minutes everyday.
If you’d like to change that interval, edit it here:
/etc/cron.d/sysstat

4. To look at your load averages, type in
sar -A

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

Tech , , , ,

Windows 7 Beta on VMWare – How to resolve Network adapter issues

January 24th, 2009

If you have installed a Windows 7 Beta as Guest operating system on a VMWare server, over a Windows XP Base, you might run into issue with Network adapter (..like i did).

If that’s the case, follow these steps.
1. Go to Windows 7 Guest OS VMWare directory on your file system, and open the .vmx file in a notepad.
2. Add the following line to the end of the file and save it.
ethernet0.virtualDev = “e1000″.
3. Restart the Windows 7 Guest Operating system.

Windows 7 will recognize the network adapter and you should be able to connect to the internet.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

Tech, Windows , , ,

OutTwit: Post your Outlook Tasks to Twitter and receive reminders

December 9th, 2008

OutTwit’s latest release (#39) is packed with a number of powerful features – For example: Ability to save sent tweets, Keyboard shortcuts for posting tweets, Ability to filter incoming tweets based on Tweet type (FriendTimeline,@replies,Direct messages) to name a few.

In addition to all of that, one feature that I find particularly useful.. is the ability to Tweet my Outlook Tasks and Appointments.
OutTwit makes this almost painfully simple.
Follow these steps:

1. Go to your Tasks folder, select a Task.

2. Select “Retweet” from OutTwit drop down menu.

3. Change the tweet text if you’d like, and hit Update.

That’s all.
Along the same lines, you can Tweet your Appointments, Email messages.

How to receive reminders via Twitter
If you would like to get more fancy, follow these steps to receive reminders for your Outlook tasks, as Direct Messages in Twitter.

In order to do this, you’ll have to add rtm as your friend on Twitter. After completing the setup, follow these steps.

1. Pick a task from your Tasks folder.

2. Invoke Retweet command from OutTwit drop down menu.

3. Prepend the tweet text with “d rtm” and specify the reminder interval as needed. For example:

Twitter will send you a reminder for your Outlook Task item, as Direct message, at the specified time.

You can find more RTM commands here.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

Tech , , , , , ,

Get your DVDs on your iPod or iPhone for free

November 22nd, 2008

Freez iPod Video Converter lets you get your DVDs on your iPod or iPhone for free, quickly and reliably.
Small Video Soft offers a whole suite of free software – Freez iPod Video Converter, Freez DVD Ripper, Freez Screen Video Capture and more.

Lifehacker offers a few ways to put DVDs to good use. Check it out.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

Tech, Utilities, Windows

How to filter tweets from your Twitter stream, using OutTwit

November 15th, 2008

In the previous post, we have seen one way to stay on the top of tweets from a specific set of people.
Let’s look at another way to achieve the same results, using Outlook’s Search Folders feature.

You can create a new “Search Folder” in Outlook and set it to display only tweets from particular senders, or only tweets that contain a particular word, or match any other criteria supported by Outlook filters.

Follow these steps:

1. Install OutTwit if you haven’t already. Complete the basic set up.
Open Outlook. Go to the mail box containing your tweets.

2. Go to “File->New->Search folder” in Outlook.
Outlook displays a “New Search Folder” window.

3. In that window, under “Select a Search Folder“, choose “Create a custom Search Folder”.

4. Next, click on the “Choose” button in that window.
Outlook opens a “Custom Search Folder” window.

5. Give a name to your Search folder. I am calling it “MyTwitterSearch“.

6. Click on the “Criteria” button to specify search terms.
In this example, i’d like to search for all the tweets that contain the words “mashable” or “Lifehacker” in them.

7. Click on OK and return to the “Custom Search Folder” window.

8. Click on “Browse” and select OutTwit’s Twitter folder. (This is the folder you’ve configured in OutTwit).

Click on OK.

9. Your search folder is ready! Go to Mailbox->SearchFolders->MyTweetsSearch.
You can now have a look at all tweets that contain the words “mashable” or “Lifehacker” in them.

This is just one example. You can play around with Outlook’s Search Folder options and tune it according to your needs.

Combine Outlook’s Search Folders functionality with OutTwit and boost up Twitter’s Signal-to-Noise ratio :)

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

TechHit , , , , ,

Create your own Twitter Group with OutTwit

November 15th, 2008

Twitter could be overwhelming, when you follow a lot of people.
Often times, you might find yourself in a deluge of tweets and it is very easy to miss tweets from your closest friends, or other sources of interest.

OutTwit offers a very simple way that lets you quickly check what your sources of interest are tweeting about.

Follow these steps to create your own Twitter group:

1. Install OutTwit if you haven’t already. Complete the basic set up.

2. Click on OutTwit drop down menu in Outlook toolbar. Select “Search/Track Keywords”.

3. Click on “New”

4. In the “Search for” text box, add the Twitter ids you are interested in, to the group.
For example: “from:Lifehacker OR from:mashable”

5. Click on “Select Folder” and assign an Outlook folder for your group. I’m calling it “MyGroup”.

6. Click on Save and return to the “Manage Searches” window, and hit “Close”

That’s it… MyGroup is ready.

Open My Group folder in Outlook and check out tweets from people in your group.

By creating your own Twitter Group in Outloook, you can filter out signal from noise. You no longer have to dig through a pile of tweets searching for tweets from your friends.

There are tons of other possibilities with OutTwit+Twitter Search integration.
Check out this page for supported search operators.

The beauty of OutTwit lies in its seamless integration with Outlook, which you probably have open all the time anyway. So, that’s one less application on your desktop.

Make use of the Search/Track feature in OutTwit and boost up Twitter’s Signal-to-Noise ratio.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

TechHit , , , ,

Full moon over San Francisco

November 13th, 2008

A picture is worth a thousand words.
Full moon over San Francisco
Bay view
Down town view

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • Facebook

Photography