Jump to content

Conceptually stuck: passing array as function parameter


mrherman

Recommended Posts

I've spent a lot of time on this, thinking I could figure it out, but I'm royally stuck.  I have a "debugging" helper function that I call when I need to know what is happening to a variable.  The results of the call are inserted into a table, which I then review for problems.

 

Here is an earlier version of my function call to "trace_log()."  It worked fine.

trace_log( file_name(__FILE__), script_name(), __FUNCTION__, __LINE__, var_name(), var_value());

 

Now I want to put the function call parameters into an array.  Here is my code:

$myarray = array( 'myfile'    => 'file_name(__FILE__)', 
                  'script'    => 'script_name()',
                  'func'      => '__FUNCTION__',
                  'line'      => '__LINE__',
                  'var_name'  => 'var_name()',
                  'var_value' => 'var_value()'
                );

  Note that script_name(), var_name(), and var_value() are user defined functions.

 

Now, I am trying to make the parameters in $myarray work within the function, but I have not been successful.  Here is how I think the code should begin:

function trace_log( $myarray )
{
   /* Connect to db */
   
   if( is_array( $myarray ) )
    {
        foreach( $myarray as $k => $v )
        { ... !!!!! CAN'T GET PAST THIS POINT !!!!!

 

Here is the code I use in the function "trace_log()" to insert the items into a table:

   $sql = "INSERT 
             INTO reg_log_trace( step, file, script, func, line, var_name, var_value )
           VALUES ( 0,'{$file}','{$script}','{$func}', {$line}, '{$var_name}',  '{$var_value}' )";

 

How can I resolve this?  Thanks for the help!

 

 

 

 

 

 

Use extract

 

function trace_log($array) {
  if (is_array($array)) {
    extract($array);
    $sql = "INSERT 
          INTO reg_log_trace( step, file, script, func, line, var_name, var_value )
          VALUES ( 0,'{$myfile}','{$script}','{$func}', {$line}, '{$var_name}',  '{$var_value}' )";
    db_query($sql);
  } ..
}

Thanks, guys!

 

Extract() is not working because the array contains various functions.  When the array is extract-ed, it echos back the function calls, not the values that they should derive.

 

For example, after extract(), the values are:

function trace_log( $a_log )
{
   if (is_array($a_log)) 
   {
      extract($a_log);
   }
   echo $file . "<br />";
   echo $script . "<br />";
   echo $func . "<br />";
   echo $line . "<br />";
   echo $var_name . "<br />";
   echo $var_value . "<br />"; ........

Prints out:

Notice: Undefined variable: file in E:\Web\HTDOCS\Learnweb\Register_login\includes\inc_functions.php on line 75
script_name()
__FUNCTION__
__LINE__
var_name()
var_value()

 

As for serialize(), I'm not clear as to how that would be helpful.  :confused:

it echos back the function calls

 

That's because you have single-quotes around the values when you build the array, making them strings. If you want the values at the time you build the array, remove the single-quotes from around each value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.