Function argument arrays

<?php

//The function for your functions.php or plugin

function test_function($args = '') {
$defaults = array(
'arg1' => 'Default Value 1',
'arg2' => 'Default Value 2',
'arg3' => 'Default Value 3',
);

$r = wp_parse_args($args,$defaults);
extract($r);

echo "arg1 = $arg1<br>";
echo "arg2 = $arg2<br>";
echo "arg3 = $arg3<br>";
}

//Call the function using two techniques:

//1 - Using an array

$args = array(
'arg1' => 'Array Value 1',
'arg2' => 'Array Value 2',
'arg3' => 'Array Value 3',
);
test_function($args);

//2 - Using a query string

test_function('arg1=Query Value 1&arg2=Query Value 1&arg3=Query Value 3');

0 Comments

You must be logged in to post a comment.