a non-geek's blog

Random notes on linux and web development

Posts Tagged ‘web development

PHP: Functions with arbitrary number of arguments

leave a comment »

Sometimes you may have to use a function with a variable number of arguments. Here is a simple method of passing a list of variables to a function:


function foo() {

// returns an array of all passed arguments
$args = func_get_args();

foreach ($args as $k => $v) {
echo "arg".($k+1).": $v\n";
}

}

foo();
/* prints nothing */

foo('hello');
/* prints
arg1: hello
*/

foo('hello', 'world', 'again');
/* prints
arg1: hello
arg2: world
arg3: again
*/

For more interesting functions and futures, read the 9 useful php functions and futures you need to know article on net.tutsplus.com.

Bookmark and Share

Written by nongeekboy

April 28, 2010 at 5:43 pm

Posted in php

Tagged with , ,

Follow

Get every new post delivered to your Inbox.

Join 202 other followers