Page 1 of 1

php functions

Posted: Sat Jun 04, 2011 4:45 pm
by Torniquet
Is there anyway to make a function which can hold an undefined amount of variables?

Not sure how to best explain this, but with the sprintf function, you can include all the variables needed to do what you need...

Code: Select all

sprintf($string, [$a], [$b], [$c]........)
 
is there a way to replicate that in created functions, so you dont have to set them on the function? or do you have to set a max amount by building the function to include them?

Code: Select all

function myFunction($string, $a='', $b='', $c='', $d='' ... etc ...)
{

}
 
hope it all makes enough sense to help me lol.

ta x

Re: php functions

Posted: Sat Jun 04, 2011 5:16 pm
by Jackolantern
You don't have to set up how many parameters you can accept when you write the function. You can just leave it blank, and use 3 different built-in functions to get the variable amount of parameters:

1. func_num_args(): this returns the number of arguments sent to the function.
2. func_get_arg(): this is used to return the arguments one at a time.
3. func_get_args(): this is used to return an array of all the arguments at once.

Also remember that you can write that your function accepts 1 parameter, but that parameter can be an array of a nearly unlimited number of arguments.

Re: php functions

Posted: Sat Jun 04, 2011 6:45 pm
by Torniquet
ahh thats awesome stuff jack thanks :) will investigate those further.

Currently i am using an array (and thinking about it might stick to it), but for what the function does, i was thinking that it might be neater doing it the other way. But looking over my code, it will probably just get bigger doing it that way lol.

Code: Select all

function create_nav($arg = array())
{
    global $dba, $cur_lang, $root_dir;
    
    include($root_dir . "_l/$cur_lang/nav_lang.php");
    $query = "SELECT * FROM pbw_navigation WHERE parent='0'";
    $sql = $dba->query($query);
    while($row = $sql->fetch_object())
    {
        $argument_arr = "";
        if(array_key_exists($row->lang_ident, $arg))
        {
            foreach($arg[$row->lang_ident] as $k => $v)
            {
                $argument .= $k . "=" . $v . "&";
            }
        }
        
        $argument = substr($argument, 0, -1);
        
        $link = ($argument != "") ? $row->page_link . "?" . $argument : $row->page_link;
        
        $query_in = "SELECT * FROM pbw_navigation WHERE parent='".$row->id."'";
        $sql_in = $dba->query($query_in);
        while($row_in = $sql_in->fetch_object())
        {
            $nav_in[] = array(
                "DISPLAY"    =>    $nav_lang[$row_in->lang_ident],
                "LINK"        =>    $row_in->page_link,
                "REQ_LOG"    =>    $row_in->req_login,
            );
        }
        
        $nav[] = array(
                "DISPLAY"    =>    $nav_lang[$row->lang_ident],
                "LINK"        =>    $link,
                "TYPE"        =>    $row->menu_type,
                "INNER"        =>    $nav_in,
                "REQ_LOG"    =>    $row->req_login,
            );
        
        unset($nav_in);
        unset($argument);
    }
    return $nav;
}
 
Thats what the code looks like using an array, then is set in motion by the following...

Code: Select all

$nav_arg = array(
        "PROFILE"    =>    array("uid"    =>    $user_info['user_id'])
    );

$smarty->assign("NAV", create_nav($nav_arg));
 
So yeah, will probably just stick to what i have.

Thanks anyway jack :) much appreciated.

Re: php functions

Posted: Sat Jun 04, 2011 6:50 pm
by Jackolantern
I prefer using the array myself. Using the 3 utility functions feels hacky and inelegant.