javascript : 7 tips for better coding

1- Use short hand notation for declaring arrays and objects

Arrays and objects are declared as following:

var someArray = new Array();
var someObject = new Object();

There is a shorthand notation for declaring these which is much more easier to use. It can be used like this:

var someArray = [];
var someObject = {};

2- Use square bracket notation – [] – to access property of an object rather then using . (dot) notation

There is a good reason why you should do this. Suppose you have an object like following:

var myObject = { "name1" : "some name" , "name2" : "another name" , "name3" : "yet another name" };

Properties of myObject can be accessed using either of the 2 ways.

var value = myObject["name2"]; // will output another name
var value = myObject.name2; // will output another name

Now assume you want to access these values dynamically (probably in a loop). Now see what happens.

var result = + i; //this is an error

var result = myObject['name' + i]; // this is good. will output correct value depending on value of i

If you take the first approach, you are in a mess. It will look for and will try to add the value of i to it and thus an error
Using the second method is safer and readable too.

3-    Break a long string into multiple lines

This surely makes code more readable. You can do it like below.

var longStr = "This is a long long "
+ "string which has been "
+ "broken into "
+ "multiple lines.";

Think there is only one way of doing so? No, Here is another(lesser known) method:

var longStr = "This is a long long \
string which has been \
broken into \
multiple lines.";

Continue reading javascript : 7 tips for better coding

Super simple way to expand bitly urls using PHP

OK. Agree that provides an API for expanding URL’s. The main purpose of our simple exercise here is to use  curl functions in PHP to achieve the same. We will write a simple function that will accept a bitly url as parameter. Then we will use curl to get the expanded url.

Below is the full code for function which we will call expandURL.

	function expandURL($url)
		$retVal = 'Error';
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		if(curl_exec($ch) != false)
			$response = curl_exec($ch);
			if($response != false)
				$responseInfo = curl_getinfo($ch);
				if($responseInfo['http_code'] == 200)
					$retVal =  'Expanded URL: '.$responseInfo['url'];
				else if($responseInfo['http_code'] == 404)
					$retVal =  'URL Not found';
					$retVal =  'HTTP error: '.$responseInfo['http_code'];
				$retVal =  curl_error($ch);
			$retVal =  'cURL error ocurred : '.curl_error($ch);
		return $retVal;

	$result = expandURL('');
	echo $result;

How this works?

$retVal is the return value from function. First we initialize a curl session for bitly url using curl_init. Next 2 lines are most important.

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Since our aim is not to display any output directly in browser, we will set CURLOPT_RETURNTRANSFER to 1 (true). It will  return the value after curl execution in a string and will not output anything to browser.

Continue reading Super simple way to expand bitly urls using PHP

5 JSON resources for web developers

JSON(Javascript Object Notation) is defined as a lightweight data interchange format and fat-free lightweight alternative to xml. Since json is the native data form of Javascript, it can be used on the browser more easily then XML.

If you are a web developer who works with javascript/ajax a lot and still wondering what this json thing is, here is the first step to start with. Read this post about json and its uses.

Below is a list of 5 resources from where you can learn about json in detail.


First place to go to find definitive information. Douglas crockford is the man who invented json. is a one stop place for everything json.

2- Mastering JSON

Detailed examples as well as retrieving and sending json data between browser and server. You cam also find tips on security and best practices.

3- JSON vs XML debate

Arguments are going on between which is better to use as a data format. Go read and judge yourself.

4- JSON with Ajax and PHP

5- Slide on JSON

FAQ(Javascript) : User confirmation on close button of browser

Problem: On clicking the close button of browser ask for user confirmation with a custom message

Solution: Use the window.onbeforeunload method. This method expects a string value. This string will be displayed to user along with the default string. Default string that is displayed to user is:

Are you sure you want to navigate away from this page?
Press OK to continue, or Cancel to stay on the current page.

You can see an example in action here. Full code is given below.

<script type="text/javascript">
  window.onbeforeunload = function()
     return 'Please do not leave this page.'; 
alternatively, you can also write the following
this will also work the same as above 
window.onbeforeunload = confirmFromUser;
function confirmFromUser()
	return 'Please do not leave this page.';

	Try reloading this page or closing your browser. <br/>
        Also try back, forward buttons.
	<a href="">Click me</a>

onbeforeunload event will fire on reloading the page, using browser’s back/forward button or even on clicking an anchor that navigates user from current page to any other location.

PHP – Turn register globals on off using htaccess


My application is hosted on a server where php has register_globals set to on in php.ini. But using register_globals = on makes code error prone and is dangerous to use.
I want to turn it of for my application regardless of what is its setting for other applications.
Since the value of register_globals cannot be set at run time bu using ini_set, how can I do it?


Use htaccess file to control this behaviour. Create a file named .htaccess in root directory of your application and write the following line in it:

php_flag register_globals off

Thats all you need to do.

You can verify this by creating a phpinfo() file. Suppose your server has register_globals set to on and you have turned it off using above syntax.

phpinfo() will show the local value as off and master value as on.

Note: Of course, this will work only if your apache configuration allows to override htaccess settings.