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:

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:

Author: admin

I like chocolate, gadgets, open source software, photography, traveling and all shades of green colors. I love spending time with fun loving friends and family members. This is my own online journal.