Archive

Archive for the ‘Tech’ Category

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);

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:


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

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.

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.

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.

Tech, Utilities, Windows

Website Monitoring Services – A comparison

November 9th, 2008

Website Monitoring Services need no introduction. There are several website monitoring services out there that provide uptime alerts and track your site’s response time.

However, if you own a business-critical website, you might need a bit more than simple uptime checks. Your website could be up, but your order forms could be broken. Your users won’t be able to place orders. And you can’t practically check these pages round the clock.

So, here’s an attempt to list a few low cost (<$10/month) website monitoring services, that provide transaction monitoring. It is not an ordered list, but PingDom seems most promising provider of the lot.

Service Transaction Monitor SSL Monitoring Price per month Free trial Number of Checks Check Resolution
PingDom Yes Yes $10 30 days 5 Configurable, between 1 & 60
WatchTour Yes Yes $3 30 days Unlimited 5
HyperSpin Yes Yes $10 15 days 5 15
AlertBot Yes Yes $9 14 days 10 5
Site24×7.com Yes Yes $1 15 days Configurable Configurable

Here are a few more options.
Some of them provide very basic uptime checks with out any transaction monitoring.
Some them provide transaction monitoring, but are a bit pricey.

Service Transacrion Monitor SSL Monitor Price/Month FreeTrial
Host-Tracker No No $5 30 days
WebsitePulse Yes Yes $15 30 days
Alertra No No Variable 30 days
SiteUptime No Yes $10 No
ServiceUptime No Yes $10 No
UpTrends Yes Yes $22 30 days
WatchMouse Yes Yes $35 30 days
UptimeParty No No Free

Tech

Twitter Squatting

November 8th, 2008

Perhaps the ultimate signs of twitter’s success & popularity!

Domain name investment is a mature industry now with every word in the English dictionary long since reserved by some entrepreneur/squatter. Now Twitter has created a new supply of valuable “names”: Twitter IDs. They take the form of twitter.com/stiennon for instance. Have you signed up for your free Twitter ID? Do you own your surname? Company name? Brand identity?
Read on..

100 World’s Greatest Twitter Tips & Twitter Secrets The Ultimate “Twitter How To” Guide

Tech ,

(Google) Earth in your pocket

October 27th, 2008

The world just got a little bit smaller, small enough to fit in your iPhone.
Google has released an iPhone version of Google Earth.
Just swipe of your finger on your iphone screen, and you can fly all around the world.

With Google Earth for iPhone, you can:
• Tilt your iPhone to adjust your view to see mountainous terrain
• View the Panoramio layer and browse the millions of geo-located photos from around the world
• View geo-located Wikipedia articles
• Use the ‘Location’ feature to fly to your current location
• Search for cities, places and business around the globe with Google Local Search

This is a truly awesome iPhone app!

The Taj Mahal
The Taj Mahal

Mount Everest
Mount Everest

Tech , ,