Collection of 10 top 10 lists about web development

I am a regular reader of web development articles and in this process bookmark a lot of them. Out of that collection I present to you a list of 10 articles which themselves are lists of 10(tips, functions etc). Not all of them are top 10 ;) but equally good. All these articles are related to php, javascript, html, mysql and web development in general.

1- 10 code snippets for PHP developers

Simple and handy functions for common tasks like validating email address and XSL transformation.

 

2- 10 Advanced PHP Tips Revisited

Detailed and excellent tips from famous PHP gurus Chris Shiflett and Sean Coates.

 

3- Top 10 PHP frameworks

PHP frameworks are quite popular among developers these days. This site is dedicated to php frameworks alone. In this article all frameworks have been compared for features. Very useful to help you in decision making if you are planning to use a framework for your next project.

 

4- Top 10 custom JavaScript functions of all time

This is my favorite one. These 10 functions are a must in every web developer’s toolbox. Functions like getElementsByClass, toggle and cookie functions are included.

 

5- 10 JavaScript Quick Tips and Best Practices

Another good article on javascript best practices.

 

6- Top 10 Firefox Addons For Web Developers

Who hasn’t heard of Firebug or GreaseMonkey. This article lists similar addons for firefox that are a great help in web development.

 


Read the rest of this entry »

ajax, javascript, php

Quick PHP Tip: How to parse CDATA sections using SimpleXML

Applies to:  simplexml_load_string and simplexml_load_file

Problem : SimpleXML does not parse text inside CDATA tags in an XML.

Consider the XML below:

 
$str = '<rootNode>';
$str.='<childNode>some text goes here</childNode>';
$str.='</rootNode>';

To parse it we use following syntax:

$xml = simplexml_load_string($str);

On printing it outputs:

 
SimpleXMLElement Object
(
    [childNode] => some text goes here
)

Thats OK. Now the same xml but this time the text is enclosed in CDATA tags.

 
$str = '<rootNode>';
$str.='<childNode><![CDATA[some text goes here]]></childNode>';
$str.='</rootNode>'

On printing this gives following output:

 
SimpleXMLElement Object
(
    [childNode] => SimpleXMLElement Object
        (
        )
 
)

Yes its empty. This is because SimpleXML does not parse CDATA tags. All data enclosed within CDATA is ignored by SimpleXML parser.

Solution: Set the 3rd parameter to LIBXML_NOCDATA while parsing.

simplexml_load_string(simplexml_load_file too) actually takes 3 parameters.

Provide the 3rd parameter LIBXML_NOCDATA and SimpleXML will consider CDATA nodes as text nodes and will parse them.

 
$xml = simplexml_load_string($str,'SimpleXMLElement', LIBXML_NOCDATA);

This will output as desired:

 
SimpleXMLElement Object
(
    [childNode] => some text goes here
)

Please note that using the third parameter requires PHP >=5.1 compiled with libxml.

I will be publishing a new post shortly on how to use SimpleXML to parse xml and extract data.

FAQ, php

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 = myObject.name + 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 myObject.name 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.";


Read the rest of this entry »

javascript

Super simple way to expand bitly urls using PHP

OK. Agree that bit.ly 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';
				}
				else
				{
					$retVal =  'HTTP error: '.$responseInfo['http_code'];
				}
			}
			else
			{
				$retVal =  curl_error($ch);
			}
		}
		else
		{
			$retVal =  'cURL error ocurred : '.curl_error($ch);
		}
		curl_close($ch);
		return $retVal;
	}
 
	$result = expandURL('http://bit.ly/LoDhO');
	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.


Read the rest of this entry »

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.

1- json.org

First place to go to find definitive information. Douglas crockford is the man who invented json. json.org 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[video]

5- Slide on JSON

ajax, javascript, json
10,926 spam comments
blocked by
Akismet