note 42021 modified in function.func-get-arg by danbrown

From: Date: Sat, 19 Jul 2008 21:36:15 +0000
Subject: note 42021 modified in function.func-get-arg by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141760@lists.php.net to get a copy of this message
A pretty cool thing for user defined functions is only to submit the needed parameters. If you call a function that has three optional parameters you have to define the two first ones (even if they should stay like the defined standard in the function) before your are able to tell the function what the third important parameter is. Instead you might as well just find out by the pattern or the type of the submitted parameter which variable it should be assigned to. like this: <?php function whatever() { for($i=0;$i<func_num_args();$i++) { if(is_bool(func_get_arg($i))) $log_ip=func_get_arg($i); if(is_int(func_get_arg($i))) $limit=func_get_arg($i); if(is_string(func_get_arg($i))) $name=func_get_arg($i); } } ?> Now you can call the function with any parameter you want. e.g.: <?php whatever(3600); ?> in that case $limit would be defined with 3600. It doesn't matter if you do this: <?php whatever(3600,false); ?> or this: <?php whatever(3600,"blaaah"); ?> or this: <?php whatever("blaaah",true,3600); ?> You may also use ereg(). Through that you're able to use more than one parameter as a string. hmm probably ereg() is the best solution... never mind. just check it out ;-) --was-- A pretty cool thing for user defined functions is only to submit the needed parameters. If you call a function that has three optional parameters you have to define the two first ones (even if they should stay like the defined standard in the function) before your are able to tell the function what the third important parameter is. Instead you might as well just find out by the pattern or the type of the submitted parameter which variable it should be assigned to. like this: <? function whatever() { for($i=0;$i<func_num_args();$i++) { if(is_bool(func_get_arg($i))) $log_ip=func_get_arg($i); if(is_int(func_get_arg($i))) $limit=func_get_arg($i); if(is_string(func_get_arg($i))) $name=func_get_arg($i); } } ?> Now you can call the function with any parameter you want. e.g.: <? whatever(3600); ?> in that case $limit would be defined with 3600. It doesn't matter if you do this: <? whatever(3600,false); ?> or this: <? whatever(3600,"blaaah"); ?> or this: <? whatever("blaaah",true,3600); ?> You may also use ereg(). Through that you're able to use more than one parameter as a string. hmm probably ereg() is the best solution... never mind. just check it out ;-) http://php.net/manual/en/function.func-get-arg.php

« previous php.notes (#141760) next »