How to create Functions that Take a variable number of arguments in PHP

Recently I was coding small application in PHP. I wrote simple function that generates the HTML code of table. Here is what I did first

startTable($col, $heading1, $heading2);

Problem with above function was clear I needed something which can very both column and heading without writing 2-3 functions. I know this can be done in ‘C’. So I opened the PHP documentation and I come across this “PHP 4 has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions.” :)

So I rewrote my startTable() as follows:


function startTable($allArgs){
	// the number of arguments passed to the function
	$size = func_num_args();

	echo '<table border=1 width=100%><tr>';
	$width=100;
	if ( $size == 2 ) $width=50;

	if ( $size == 3 ) $width=33;

	if ( $size == 4 ) $width=25;

        // iterate through the arguments and add up the numbers
	for ($i = 0; $i < $size; $i++) {

		$colTitle = func_get_arg($i);
		echo "<td width=$width% align=center bgcolor=#BFBFBF>$colTitle</td>";

	}
	echo "</tr>";
}

Basically func_num_args() returns the number of arguments passed to the function and func_get_arg($i) return an item from the argument list. Finally I endup creating addRow as follows:


function addRow($data){

	$size= func_num_args();
	$width=100;
	if ( $size == 2 ) $width=50;

	if ( $size == 3 ) $width=33;

	if ( $size == 4 ) $width=25;

	echo "<tr>";
	for ($i = 0; $i < $size; $i++) {

		$align="";
		$colData = func_get_arg($i);

		$tmp=split("\|","$colData");
		$align = current($tmp);

		$text =  end($tmp);
		if ($align == $text ) $align="left";

		echo "<td width=$width% align=$align>$text</td>";
	}
	echo "</tr>";

}
?>

Discovery – Spaced out

Ok the safe return of Discovery and its crew gives a sigh of relief to everyone. But one question remained unanswered was it necessary to risk the astronauts in the first place? Ok space technology is essential but sending human in space… I don’t think so it was necessary. We have lots of remote devices/tool such as robots that can be used.
Getting knowledge of universe it essential (especially when you are considered as advanced and successful if you are leader in space technology) in long term and one can surly use the technology to replace human in danger places like this.

I’m also against president kalam view to send an Indian on the moon. C’mon Mr. President, we have more to fix than this *stupid* idea of sending an Indian out there. Please fix or create new technology that can forecast earth quakes, tsunamis and floods warning. Please don’t waste money of honest tax payers on this and if you really would like to send an Indian there then start with robots!

I will be interested to get more views from others.

What is True life?

Recently I came across Swamin Sukhabodhananda little thought. According to him

“True life is filled with insecurities, offering unknown surprises”

I think insecurities in life leads to adventures life :D

Further he said

“False life is filled with expectations of securities. Life is insecure and there lies the beauty of life”

Again I think false life leads to bad expectations from life which finally leads you to dark and depression. So just enjoy life as it comes to you don’t think of too much of your future and securities.

We are the web

Ten years ago the World Wide Web came into being for most of us, and
Wired Magazine is marking what has been a remarkable decade of life
changing technology with a series of articles in its August issue. Today we see blogs, wikipedia, open source projects, peer-to-peer networks behold the power of the people.
It may seem obvious now, but those who first imagined the Web had an
uphill battle to persuade people that it was possible – and would be
useful. Kevin Kelly’s “We Are the Web” details the public and
democratic nature of what we now rely on daily. And guess what by 2015 everyone alive will (on average) write a song, author a book, make a video, craft a weblog, and code a program. 175,000 books were published and more than 30,000 music albums were released in the US last year and at the same time 14 million blogs lauanced worldwide. Sure we are the web and future seems to be bright!

Cell phone for less than Rs.1000 in near future

Texas Instruments new chip will make this possible. We have already seen massive growth in Cell phone market in India and now with such new technology will go one step ahead. Well job done by TI’s Indian R & D team :D
Read more at biz.yahoo.com.

DesktopBSD is out!

DesktopBSD combines the stability of FreeBSD, the usability and functionality of KDE and the simplicity of specially developed software to provide a system that’s easy to use and install. Take a look at screenshots
. More info available at offical web page.

Big B – What a Man

Amitabh Bachchan aka Big B still leads the race of the industry’s top starts. He ranks amongst best starts and most prolific brand ambassadors. Today he is in sixties and the big B still rocks with TV & cinema. This year so far his six films are released. Tonight countries biggest game TV show “Kaun Banega Crorepati” (KBC2) he will be back in action again.