World’s Most Popular Programming Languages

“Look, see those birds? At some point a program was written to govern them. A program was written to watch over the trees, and the wind, the sunrise, and sunset. There are programs running all over the place. The ones doing their job, doing what they were meant to do, are invisible. You’d never even know they were here.”
The Oracle in The Matrix – Reloaded

 
We are able to view data from all over the world, chat with people on any continent, play games with or against any of our friends without leaving the comfort of our homes etc – All this is possible due to programs written using languages which remain invisible to the user.
Such is the brilliance of the whole system we call software & I am thankful for being part of this line of work :) .

The question ‘Which are the most popular programming languages?’ is important for people who are curious to know where their own programming language of choice stands in the battle for popularity. It even helps those of us who want to learn a new language & a small peep at what their colleagues all over the world favour might be the little push they desire.

Note : I wish to compare only languages which are used at the server side. Thus it excludes client side scripting languages like Javascript & markup languages like HTML, XML.


The TIOBE Programming Index is probably the only index which keeps track of the popularity of a programming language. Its updated every month. Thats why its regularly quoted by many articles.


Its Rules state that the ratings are calculated by counting hits of the most popular search engines for the search query +”<language> programming” .
Though I agree with the result (atleast with the top 4) , I don’t agree with the methodology used by the survey. The flaw can be seen if we replace the word ‘programming’ with ‘language’ since ‘programming’ word is not actually regularly used when we reference languages like php.
On google search – “php language” beats all other combinations easily (3Mil +) …even “java language” isnt able to match it (600k) though its aided by results of the place java & its language.

A better methodology is used Here though it stopped being updated after Oct 2008. The language name is combined with a number of relevant words so as to give a more valid result. The PERL code used by the site is available. Hopefully at some point of time it will be reused for getting the appropriate results.


While looking at the server side programming languages, I tried another approach at comparison. I took the Top 10 most popular sites & listed out the language used in them. The results were mighty interesting :wink: .
 
Top 10 Sites (as per Alexa)

  1. Google (Help/feedback sections are clearly Python pages..but main site?)
  2. Facebook (PHP)
  3. Youtube (PHP)
  4. Yahoo (PHP)
  5. Live (ASP.NET)
  6. Baidu (PHP)
  7. Wikipedia (PHP)
  8. Blogger (???)
  9. MSN (ASP.NET)
  10. Twitter (Said to be Ruby On Rails)

Note :
(1) Our very own WordPress is made with PHP.
(2) Google is believed to use an array of languages for its site. I wont be surprised if it has developed its own custom language based on Python & C :P .

In this little survey, PHP has come out the winner. Java & ASP.NET have more reputed sites among them (powerful companies are its developers
+ a dedicated support system ensures that big sites always use JAVA/.NET especially for financial transactions) but the open source languages like PHP, Python have the numbers(huge number of small scale sites) & luckily for PHP, some of the large scale sites using it have become so renowned that they are right there lying smug on top of the popularity list :cool: .


Ref :
Debate : Languages used in Google & other popular sites
Various Languages List
Tiobe Stats Criticism


ZEND Certified PHP 5 Engineer – Thats Me !


Why The ZEND PHP Certification ?


  1. I like learning. Nothing like an exam to make one explore the depth of a subject.
  2. A certification helps in asserting a neutral organization’s opinion about how good your knowledge is. If I had to test my knowledge in a certification exam , it had to be PHP since I have been working with it for quite some time.
  3. I thought the ZCE Logo would look good on my papers :P

Study Material


  • PHP Architects ZEND PHP 5 Study guide PDF.
  • PHP Architects Zend PHP Certification Practice Test Book PDF (its for PHP 4 but contains some very good questions).

Preparation


  • I prepared my own short notes from the Study Guide which contained the important stuff from each chapter.
  • The Study Guide mentions lots of stuff……but I never took any line by its face value. I tested each functionality myself as I read it. Since the books contains quite a few mistakes, its best to try it out yourself.

Exam Experience


  • Its not permitted to reveal the questions of the exam so I shall only comment in general on it.
  • The exam wasn’t easy. Why? Imagine this – You learn how the concept A works. then you learn about concept B. The exam has questions that require the output if both concepts A & B are used together !
  • I was done with the exam in an hour. I went through the questions (the doubtful ones) which I had marked for review for the remaining 30 min.
  • I was ready for another week of studying & another attempt at this exam when I was pleasantly surprised by the ‘Congratulations’ message :-)

Lessons Learn’t


  • You can be aware of a number of such complex scenarios of PHP if you encountered them while working on some project involving PHP. I was…& it helped a lot.
  • Its apparent that a curious person must have had a hand in creating the database of questions since only such a person would think of the consequences of mixing different concepts with each other. I am curious by nature…& my frequent trials of concepts helped me.
  • I only wish I had gone through the string, array & stream functions listed in php.net site. That would have reduced the tense moments I had during the exam. I was so enamoured by XML, that I spent a lot of time on it. It didn’t help me in the exam but it did make me a big fan of XML :D.
  • All in all , It was a good exam. I was a little unsure of about 50% of the questions. My reaction to some questions was : ‘Oh..thats a different way to look at things :!: ‘ .After the exam, I cleared those doubts using my trusty comp.
  • It was nice to learn something new. That is how an exam should be. It should challenge your boundaries of knowledge & also encourage you to learn more. Thank You ZEND. We shall probably meet again after PHP 6 :)

Update :
Though the site gives you a time frame of 8 weeks, the ZEND Certificate (hard copy) reached me in only 2 weeks !


PHP : XML Create-Add-Edit-Modify using DOM , SimpleXML , XPath

Over the last few working days, I spent a quite a bit of time playing around with XML. While searching through the net, I found few comprehensive PHP XML guides. There never was a ‘1 stop all operations’ guide for learning XML.
As such I decided to club together examples of all kinds of operations I ever did on XML in a single post. I hope it benefits others out there who wish to learn more about XML manipulation.
Note : Since the post got quite large, I decided to only use the Tree Map style parsers – DOM & Simple XML.

XML Pic

Operations Performed:
(1) Create XML OR Array to XML Conversion OR CDATA Element Eg
(2) Edit XML – Edit/Modify Element Data (accessed serially)
(3) Edit XML – Edit specific Elements (accessed conditionally)
(4) Edit XML – Element Addition (to queue end)
(5) Edit XML – Element Addition (to queue start)
(6) Edit XML – Element Addition (before a specific node)
(7) Delete Elements (accessed serially)
(8) Delete Elements (accessed conditionally)
(9) Rearrange / Reorder Elements
(10) Display Required data in XML Form itself OR Remove all children nodes save one OR Copy/Clone Node Eg OR Compare/Search non numeric data (like date or time) to get result.

library.xml will be used in all operations.
ps : I have added the indention & spaces outside the tags in the below xml for a presentable xml form.
Remove them before saving your xml file else most of the usual XML functions wont work in the desired manner.

<?xml version="1.0"?>
<library>
    <book isbn="1001" pubdate="1943-01-01">
        <title><![CDATA[The Fountainhead]]></title>
        <author>Ayn Rand</author>
        <price>300</price>
    </book>
    <book isbn="1002" pubdate="1954-01-01">
        <title><![CDATA[The Lord of the Rings]]></title>
        <author>J.R.R.Tolkein</author>
        <price>500</price>
    </book>
    <book isbn="1003" pubdate="1982-01-01">
        <title><![CDATA[The Dark Tower]]></title>
        <author>Stephen King</author>
        <price>200</price>
    </book>
</library>

#######################################
// (1) Create XML OR
Array to XML Conversion OR
CDATA Element Eg
#######################################

// (i) SimpleXML :

// Cant create CDATA element for title in SimpleXML.
function fnSimpleXMLCreate()
    {
        $arr = array(array('isbn'=>'1001', 'pubdate'=>'1943-01-01', 'title'=>'The Fountainhead',
                               'author'=>'Ayn Rand', 'price'=>'300'),
                         array('isbn'=>'1002', 'pubdate'=>'1954-01-01',
                               'title'=>'The Lord of the Rings', 'author'=>'J.R.R.Tolkein',
                               'price'=>'500'),
                         array('isbn'=>'1003', 'pubdate'=>'1982-01-01', 'title'=>'The Dark Tower',
                               'author'=>'Stephen King', 'price'=>'200'));
        
        $library = new SimpleXMLElement('<library />');
        
        for($i=0;$i<3;$i++)
        {
            $book = $library->addChild('book');
            $book->addAttribute('isbn', $arr[$i]['isbn']);
            $book->addAttribute('pubdate', $arr[$i]['pubdate']);
            $book->addChild('title', $arr[$i]['title']); //cant create CDATA in SimpleXML.
            $book->addChild('author', $arr[$i]['author']);
            $book->addChild('price', $arr[$i]['price']);
        }
       
        $library->asXML('library.xml');
    }


// (ii) DOM :

function fnDomCreate()
    {
       $arr = array(array('isbn'=>'1001', 'pubdate'=>'1943-01-01', 'title'=>'The Fountainhead',
                               'author'=>'Ayn Rand', 'price'=>'300'),
                         array('isbn'=>'1002', 'pubdate'=>'1954-01-01',
                               'title'=>'The Lord of the Rings', 'author'=>'J.R.R.Tolkein',
                               'price'=>'500'),
                         array('isbn'=>'1003', 'pubdate'=>'1982-01-01', 'title'=>'The Dark Tower',
                               'author'=>'Stephen King', 'price'=>'200'));
        
        $dom = new DOMDocument();
        $library = $dom->createElement('library');
        $dom->appendChild($library);
        
        for($i=0;$i<3;$i++)
        {
            $book = $dom->createElement('book');
            $book->setAttribute('isbn',$arr[$i]['isbn']);
             $book->setAttribute('pubdate',$arr[$i]['pubdate']);
            
            //$prop = $dom->createElement('title', $arr[$i]['title']);
            $prop = $dom->createElement('title');
            $text = $dom->createCDATASection($arr[$i]['title']);
            $prop->appendChild($text);
            $book->appendChild($prop);
            
            $prop = $dom->createElement('author', $arr[$i]['author']);
            $book->appendChild($prop);
            $prop = $dom->createElement('price', $arr[$i]['price']);
            $book->appendChild($prop);
            $library->appendChild($book);
        }
        //header("Content-type: text/xml");
        $dom->save('library.xml');
    }

#######################################
// (2) Edit XML – Edit/Modify Element Data (accessed serially)
#######################################

// (i) SimpleXML :

// Edit Last Book Title
function fnSimpleXMLEditElementSeq()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $num = count($library);
        $library->book[$num-1]->title .= ' - The Gunslinger';
        header("Content-type: text/xml");
        echo $library->asXML();
    }


// (ii) DOM :

//Edit Last Book Title
    function fnDOMEditElementSeq()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $cnt = $library->childNodes->length;
        
        $library->childNodes->item($cnt-1)->getElementsByTagName('title')->item(0)->nodeValue .= ' Series'; 
       // 2nd way #$library->getElementsByTagName('book')->item($cnt-1)->getElementsByTagName('title')->item(0)->nodeValue .= ' Series';
       
       //3rd Way
       //$library->childNodes->item($cnt-1)->childNodes->item(0)->nodeValue .= ' Series';
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (3) Edit XML – Edit specific Elements (accessed conditionally)
#######################################

// (i) SimpleXML :

//Edit Title of book with author J.R.R.Tolkein
    function fnSimpleXMLEditElementCond()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->xpath('/library/book[author="J.R.R.Tolkein"]');
        $book[0]->title .= ' Series';
        header("Content-type: text/xml");
        echo $library->asXML();
    }


// (ii) DOM (with XPath):

 //Edit Title of book with author J.R.R.Tolkein
    function fnDOMEditElementCond()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $xpath = new DOMXPath($dom);
        $result = $xpath->query('/library/book[author="J.R.R.Tolkein"]/title');
        $result->item(0)->nodeValue .= ' Series';
        // This will remove the CDATA property of the element.
        //To retain it, delete this element (see delete eg) & recreate it with CDATA (see create xml eg).
        
        //2nd Way
        //$result = $xpath->query('/library/book[author="J.R.R.Tolkein"]');
       // $result->item(0)->getElementsByTagName('title')->item(0)->nodeValue .= ' Series';
        header("Content-type: text/xml");
        echo $dom->saveXML();
        
    }

#######################################
// (4) Edit XML – Element Addition (to queue end)
#######################################

// (i) SimpleXML :

//Add another Book to the end
    function fnSimpleXMLAddElement2End()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->addChild('book');
        $book->addAttribute('isbn', '1004');
        $book->addAttribute('pubdate', '1960-07-11');
        $book->addChild('title', "To Kill a Mockingbird");
        $book->addChild('author', "Harper Lee");
        $book->addChild('price', "100");
        header("Content-type: text/xml");
        echo $library->asXML();
    }


// (ii) DOM :

    //Add another Book to the end
    function fnDOMAddElement2End()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        
        $book = $dom->createElement('book');
        $book->setAttribute('isbn','1000');
        $book->setAttribute('pubdate','1960-07-11');
        
        $prop = $dom->createElement('title');
        $text = $dom->createTextNode('To Kill a Mockingbird');
        $prop->appendChild($text);
        $book->appendChild($prop);
        
         $prop = $dom->createElement('author','Harper Lee');
        $book->appendChild($prop);
        $prop = $dom->createElement('price','100');
        $book->appendChild($prop);
        
        $library->appendChild($book);
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
//(5) Edit XML – Element Addition (to queue start)
#######################################

// (i) SimpleXML :

// Add a Book to List Start
// Insert Before Functionality not present in SimpleXML 
// We can integrate DOM with SimpleXML to do it.
    function fnSimpleXMLAddElement2Start()
    {
        $libSimple = new SimpleXMLElement('library.xml',null,true);
        $libDom = dom_import_simplexml($libSimple);
        
        $dom = new DOMDocument();
        //returns a copy of the node to import
        $libDom = $dom->importNode($libDom, true);
        //associate it with the current document.
        $dom->appendChild($libDom);
        
        fnDOMAddElement2Start($dom); //see below DOM function
    }


// (ii) DOM :

function fnDOMAddElement2Start($dom='')
    {
        if(!$dom)
        {
            $dom = new DOMDocument();
            $dom->load('library.xml');
        }
        $library = $dom->documentElement;
        #var_dump($library->childNodes->item(0)->parentNode->nodeName);
        $book = $dom->createElement('book');
        $book->setAttribute('isbn','1000');
        $book->setAttribute('pubdate','1960-07-11');
         
        $prop = $dom->createElement('title','To Kill a Mockingbird');
        $book->appendChild($prop);
         $prop = $dom->createElement('author','Harper Lee');
        $book->appendChild($prop);
         $prop = $dom->createElement('price','100');
        $book->appendChild($prop);
        
        $library->childNodes->item(0)->parentNode->insertBefore($book,$library->childNodes->item(0));
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (6) Edit XML – Element Addition (before a specific node)
#######################################

// (i) SimpleXML :

// Add a Book Before attribute isbn 1002
    // Insert Before Functionality not present in SimpleXML 
    // We can integrate DOM with SimpleXML to do it.
    function fnSimpleXMLAddElementCond()
    {
        $libSimple = new SimpleXMLElement('library.xml',null,true);
        $libDom = dom_import_simplexml($libSimple);
        
        $dom = new DOMDocument();
        //returns a copy of the node to import
        $libDom = $dom->importNode($libDom, true);
        //associate it with the current document.
        $dom->appendChild($libDom);
        
        fnDOMAddElementCond($dom); //see below DOM eg.
    }


// (ii) DOM :

// Add a Book Before isbn 1002
    function fnDOMAddElementCond($dom='')
    {
        if(!$dom)
        {
            $dom = new DOMDocument();
            $dom->load('library.xml');
        }
        $library = $dom->documentElement;
        
        $book = $dom->createElement('book');
        $book->setAttribute('isbn','1000');
        $book->setAttribute('pubdate', '1960-07-11');
        
        $prop = $dom->createElement('title','To Kill a Mockingbird');
        $book->appendChild($prop);
         $prop = $dom->createElement('author','Harper Lee');
        $book->appendChild($prop);
        $prop = $dom->createElement('price','100');
        $book->appendChild($prop);
        
        $xpath = new DOMXPath($dom);
        $result = $xpath->query('/library/book[@isbn="1002"]');
        $library->childNodes->item(0)->parentNode->insertBefore($book,$result->item(0));
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (7) Delete Elements (accessed serially)
#######################################

// (i) SimpleXML :

// Delete 2nd book
    function fnSimpleXMLDeleteSeq()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        //$library->book[1] = null; // this only empties content
        unset($library->book[1]);
        header("Content-type: text/xml");
        echo $library->asXML();
        
    }


// (ii) DOM :

// Delete 2nd Book
    function fnDOMDeleteSeq()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        
        $library->childNodes->item(0)->parentNode->removeChild($library->childNodes->item(1));
        
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (8) Delete Elements (accessed conditionally)
#######################################

// (i) SimpleXML :

// Delete a book with  200<price<500
    // Not possible to delete node found via XPath in SimpleXML. See below.
    function fnSimpleXMLDeleteCond()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->xpath('/library/book[price>"200" and price<"500"]');
        
        //Problem here....not able to delete parent node using unset($book[0]);
        // unset of parent node only works when accessing serially. eg : unset($library->book[0]);
        
        //header("Content-type: text/xml");
        //echo $library->asXML();
        
    }


// (ii) DOM :

// Delete the book with  200<price<500 
    function fnDOMDeleteCond()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $xpath = new DOMXPath($dom);
        $result = $xpath->query('/library/book[price>"200" and price<"500"]');
        $result->item(0)->parentNode->removeChild($result->item(0));
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (9) Rearrange / Reorder Elements
#######################################

// (i) SimpleXML :

// Exchange Position of 2nd book with 3rd : fnSimpleXMLRearrange(2,3);
// Due to absence of an inbuilt function (DOM has it), we have to make our own function in SimpleXML.
//Better to use DOM.
function fnSimpleXMLRearrange($num1,$num2)
{
     $libSimple= new SimpleXMLElement('library.xml',null,true);
     //$library->book[3] = $library->book[0]; // this doesnt work
 
    $libDom = dom_import_simplexml($libSimple);

    $dom = new DOMDocument();
    //returns a copy of the node to import
    $libDom = $dom->importNode($libDom, true);
    //associate it with the current document.
    $dom->appendChild($libDom);

    fnDOMRearrange($num1,$num2,$dom); // see below DOM function
}


// (ii) DOM :

// Exchange Position of 2nd book with 3rd : fnDOMRearrange(2,3);
function fnDOMRearrange($num1,$num2,$dom=0)
{
    if(!$dom)
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
    }
    $dom = new DOMDocument();
    $dom->load('library.xml');
    $library = $dom->documentElement;
	$cnt = $library->childNodes->length;
	//echo '<br> count : ' . $cnt;

    if ($num1 > $num2) { // ensure $num1 always is less than $num2
        $num3 = $num1;
        $num1 = $num2;
        $num2 = $num3;						
    }
     
    $index1 = fnDOMConvIndex($num1,  $cnt);
    $index2 = fnDOMConvIndex($num2,  $cnt);
    /*for ($iter = 0; $iter <= $cnt; $iter++) {
        echo '<hr>' . $iter . '<br>' .  $library->childNodes->item($iter)->nodeValue;
    }*/
            
    $library->childNodes->item(0)->parentNode->insertBefore($library->childNodes->item($index1), $library->childNodes->item($index2)); // put x before y
    $library->childNodes->item(0)->parentNode->insertBefore($library->childNodes->item($index2), $library->childNodes->item($index1)); // put y before x 

    header("Content-type: text/xml");
    echo $dom->saveXML();
}
function fnDOMConvIndex($num,  $cnt)
{
    // echo '<br>' . $num;
    if ($num != 1) {
        $maxIndex = floor($cnt/2);
        //echo '<br>max: ' . $maxIndex;
        if ($maxIndex == $num) {
            $num = $cnt-2;
        } else {
            $num = ($num%2)?($num+2):($num+1); //Always odd index due to nature of DOM Element Index.
        }
    }         
    
    // echo '<br>' . $num;
    return $num;
}

#######################################
// (10) Display Required data in XML Form itself OR
Remove all children nodes save one OR
Copy/Clone Node Eg OR
Compare/Search non numeric data (like date or time) to get result.
#######################################

// (i) SimpleXML :

// Display Books published after 1980 in XML Form itself.
// No function to copy node directly in SimpleXML.
// Its simpler for this functionality to be implemented in DOM.
    function fnSimpleXMLDisplayElementCond()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->xpath('/library/book[translate(@pubdate,"-","")>translate("1980-01-01","-","")]');
        // Manually create a new structure then add searched data to it (see create xml eg.)
    }


// (ii) DOM :

// Display Books published after 1980 in XML Form itself.
    function fnDOMDisplayElementCond()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $xpath = new DOMXPath($dom);
        
        // Comparing non numeric standard data
        $result = $xpath->query('/library/book[translate(@pubdate,"-","")>translate("1980-01-01","-","")]');
        // For simpler search paramater use this :
        //$result = $xpath->query('/library/book[author="J.R.R.Tolkein"]');
        
        // Copy only node & its attributes not its contents.
        $library = $library->cloneNode(false);
        // Add the 1 element which is search result.
        $library->appendChild($result->item(0));
        
        header("Content-type: text/xml");
        echo $dom->saveXML($library);
    }

Lessons Learn’t :

  • SimpleXML is fantastic for those who will only briefly flirt with XML (or beginners) & perform simple operations on XML.
  • DOM is an absolute necessity for performing complex operations on XML data. Its learning curve is higher than SimpleXML off course but once you get the hang of it , you will know its very logical.
  • Use XPath for conditional access to data. For serial access (like last book) XPath is not needed (but u can use it) since I can use normal DOM / SimpleXML node access.

Ref :
IBM : XML Guide for PHP Developers – Part 2
http://www.devx.com/webdev/Article/41975/1954


PHP : XML QueryLog (Add/Edit)

Logging is absolutely essential to keep track of your system.
I created the below function to keep track of any database queries which failed. Since 1 of the reasons for this could be a loss of database connectivity, keeping a XML based log was more logical than a database log.

For further examples of adding/editing a XML file, check out my other post :
PHP : Create Add Edit Modify Search a XML File via SimpleXML , DOM & XPath

Note :

  • Folder Structure :
    The XML file will have be in a folder in which Y-M-d structure will be maintained. (eg : Today is 2010-05-29 so the XML file will be in inside LOGS/2010/05/29).
  • File Structure :
    <?xml version="1.0"?>
    <querylog>
        <query>
            <timestamp>2008-01-12 19:05:17</timestamp>
            <query>SELECT a,b FROM master WHERE flag='1</query>
            <error>You have an error in your SQL syntax</error>
            <serverip>182.178.2.35</serverip>
            <hostip>122.111.123.121</hostip>
            <dbserver>182.178.2.57:1204</dbserver>
            <dbname>testdb</dbname>
            <filename>/data/test.php</filename>
        </query>
    </querylog>

Code :

/**
*	fn_QueryLog()
*	Internally called to Log Query.
*      	@author 	Rohit (quest4knowledge.wordpress.com)
*
*	@param   	string	$sql	Query
*	@param  	string	$path	Existing Folder of Query Logging
*	@return 	NULL
*/
function fn_QueryLog($sql,$path='')
{
    if(!$path){
        $path 		= rtrim($_SERVER['DOCUMENT_ROOT'],'/').'/LOGS';
    }
    $folders 	= date("Y")."/".date("m")."/".date("d");
    $path       = folderCreator($path,$folders); //defined below
    $filename 	= $path."/querylog.xml"; //XML File Name
    
    $arr['timestamp'] 	= date('Y-m-d H:i:s');
    $arr['query'] 	= $sql;
    $arr['error'] 	= @mysql_error();
    $arr['serverip'] 	= $_SERVER['SERVER_ADDR'];
    $arr['hostip'] 	= $_SERVER['REMOTE_ADDR'];
    $arr['dbserver'] 	= $this->dbserver; // Your DB Server name
    $arr['dbname'] 	= $this->dbname;	// Your DB Name
    $arr['filename'] 	= $_SERVER['SCRIPT_NAME'];
    
    $dom = new DOMDocument('1.0');
    if(file_exists($filename)){ //Edit Existing XML File
        $dom->load($filename);
        $root   = $dom->documentElement;
    }else{ //Create XML File if it doesnt exist
        $dom->formatOutput = true;
        $root = $dom->createElement( "querylog" );
        $dom->appendChild($root);
        //chmod($path."/querylog.xml", 0777);
    }
    
    $q = $dom->createElement("query");
    foreach($arr as $key=>$value)
    {
        $element = $dom->createElement($key);
        if($key=='query'||$key=='error'){
            $element->appendChild($dom->createCDATASection($value));	
        }else{
            $element->appendChild($dom->createTextNode($value));
        }
        $q->appendChild( $element );
    }
    $root->appendChild($q);
    $dom->save($filename);
}

/**
*	folderCreator
*	Internally called to Create Folders if they dont exist.
*      	@author 	Rohit (quest4knowledge.wordpress.com)
*
*	@param   	string	$pathExisting	Existing Folder structure..
*	@param  	string	$dirToCreate	Folders to be created.
*	@return 	string	$pathExisting	New Existing Folder structure.
*/
function folderCreator($pathExisting,$dirToCreate)
{
    $getDir = explode("/",$dirToCreate);
    $dirCnt = count($getDir);
    for($i=0;$i<$dirCnt;$i++)
    {
        if($getDir[$i])
        {
            $pathExisting = $pathExisting.&quot;/&quot;.$getDir[$i];
            $pathExisting = str_replace('//','/',$pathExisting);
                if(!is_dir($pathExisting))	
                    if(!mkdir($pathExisting,0777))
                        die($pathExisting);
        }
    }
    return ($pathExisting);
}

PHP : Types of Sorting

Sorting is any process of arranging items in some sequence and/or in different sets.
It aids in the process of organizing data into a presentable format.

There are many sorting algorithms in existance .
(see them in action at : http://www.sorting-algorithms.com ).
PHP uses Quick Sort in implementing most types of sorting.

PHP offers a variety of options by which 1 can sort data into a number of formats.
A look at some regularly used sorting functions :

Legend : Green indicates Optional Values.

  1. sort() :
    • Syntax : sort ( $array,$sort_flags)
    • Action : Elements will be arranged from lowest to highest when this function has completed.
    • Returns : Boolean (TRUE or FALSE) depending on success
    • $sort_flags values :
      • SORT_REGULAR – Default Mode.compare items normally (don’t change types)
      • SORT_NUMERIC – compare items numerically
      • SORT_STRING – compare items as strings
    • Keys are lost after sort() is used.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : sort($fruits);
      Output Array :
      fruits[0] = apple
      fruits[1] = banana
      fruits[2] = lemon
      fruits[3] = orange
    • When this function is used for a 2 D Array, then it sorts by the value in the 1st index of the level 2 of the array(below eg it will sort by value in column ‘data1′ & only the indices in the 1st level will be lost.
    • Eg :
      Input 2 D Array :
      fruits[‘a’][‘data1′] = “222”
      fruits[‘a’][‘data2′] = “123”
      fruits[‘b’][‘data1′] = “111”
      fruits[‘b’][‘data2′] = “456”
      Action : sort($fruits);
      Output Array :
      fruits[0][‘data1′] = “111”
      fruits[0][‘data2′] = “456”
      fruits[1][‘data1′] = “222”
      fruits[1][‘data2′] = “123”
  2. asort() :
    • Syntax : asort ( $array,$sort_flags)
    • Exactly similar to the above mentioned sort() function except that keys are not lost.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : asort($fruits);
      Output Array :
      fruits[‘d’] = apple
      fruits[‘c’] = banana
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
  3. rsort() :
    • Syntax : rsort ( $array,$sort_flags)
    • Exactly similar to the above mentioned sort() function except that this function sorts an array in reverse order (highest to lowest).
    • Keys are lost after sort() is used.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : rsort($fruits);
      Output Array :
      fruits[0] = orange
      fruits[1] = lemon
      fruits[2] = banana
      fruits[3] = apple
    • When this function is used for a 2 D Array, then it sorts by the value in the 1st index of the level 2 of the array(below eg it will sort by value in column ‘data1′ & only the indices in the 1st level will be lost.
    • Eg :
      Input 2 D Array :
      fruits[‘a’][‘data1′] = “111”
      fruits[‘a’][‘data2′] = “456”
      fruits[‘b’][‘data1′] = “222”
      fruits[‘b’][‘data2′] = “123”
      Action : sort($fruits);
      Output Array :
      fruits[0][‘data1′] = “222”
      fruits[0][‘data2′] = “123”
      fruits[1][‘data1′] = “111”
      fruits[1][‘data2′] = “456”
  4. arsort() :
    • Syntax : arsort ( $array,$sort_flags)
    • Exactly similar to the above mentioned rsort() function except that keys are not lost.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : arsort($fruits);
      Output Array :
      fruits[‘b’] = orange
      fruits[‘a’] = lemon
      fruits[‘c’] = banana
      fruits[‘d’] = apple
  5. ksort() :
    • Syntax : ksort ( $array,$sort_flags)
    • Exactly similar to the above mentioned sort() function except that here it Sorts an array by key, maintaining key to data correlations.
    • Eg :
      Input Array :
      fruits[‘b’] = lemon
      fruits[‘c’] = orange
      fruits[‘a’] = banana
      fruits[‘d’] = apple
      Action : ksort($fruits);
      Output Array :
      fruits[‘a’] = banana
      fruits[‘b’] = lemon
      fruits[‘c’] = orange
      fruits[‘d’] = apple
  6. krsort() :
    • Syntax : krsort ( $array,$sort_flags)
    • Exactly similar to the above mentioned ksort() function except that here it Sorts an array by key in reverse order, maintaining key to data correlations.
    • Eg :
      Input Array :
      fruits[‘b’] = lemon
      fruits[‘c’] = orange
      fruits[‘a’] = banana
      fruits[‘d’] = apple
      Action : krsort($fruits);
      Output Array :
      fruits[‘d’] = apple
      fruits[‘c’] = orange
      fruits[‘b’] = lemon
      fruits[‘a’] = banana
Summary of Sorting Functions in PHP
Function name Sorts by Maintains
Key Relation
Order of sort
array_multisort() value associative yes,

numeric no

first array or sort options
asort() value yes low to high
arsort() value yes high to low
natcasesort() value yes natural, case insensitive
natsort() value yes natural
rsort() value no high to low
shuffle() value no random
sort() value no low to high
uasort() value yes user defined
usort() value no user defined
krsort() key yes high to low
ksort() key yes low to high
uksort() key yes user defined


Ref :
http://us.php.net/manual/en/array.sorting.php