I need to create a function which will have large amount of parameters can be null or value. Now I am creating function as below example:
function container($args){
$args += array(
'limit' => 10,
'container' => null,
'container_class' => null,
'list_class' => null,
);
echo'<'.$args['container'].' class="'.$args['container_class'].'" >';
echo 'My function will have other content here with the '.$args['limit'];
echo '<ul class="'.$args['list_class'].'" >';
echo '<li>list itme here</li>';
echo '</ul>'
echo '</'.$container.'>';
}
This is fine if I have to pass 4-5 value in array but what if I have more than 15-20 key to pass? There must be some appropriate way to achieve.
So how can I create a function in such efficient way to pass many array key as a parameters?
Thanks a lot
15-20 keys is not really a big array. When you need to think about how to pass something into args it’s when you have 1000+ keys.
Anyway you can use “Passing by reference” concept (see http://php.net/manual/en/language.references.pass.php). This concept is useful when you have to modify the variable into function without any returning statment.
But to achieve this goal, instead of sending a copy of your variable, PHP just send a key that reference the variable in memory.
And this key does not depend of the size of the variable.
How to use? By using & symbol in your function declaration
ex:
But if you use “Passing by reference” concept be careful to do not modify the $args or $args will be modify on the main script.
Final world: if you have less than 50 keys do not care about how to pass your args, just use by copy (default behavior) will be safer!