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!

 

 

 

 

 

 

Link to comment
Share on other sites

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);
  } ..
}

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.