0

I have following code to backtrace the error ....but its

$traces = debug_backtrace();

foreach ($traces as $k => $v)
{
    if ($v['function'] == 'include' 
        || $v['function'] == 'include_once' 
        || $v['function'] == 'require_once' 
        || $v['function'] == 'require')
    {
        $args = ''; 
        if (isset($v['args']) && is_array($v['args']))
        {
            $size = count($v['args']);
            foreach ($v['args'] as $key => $arg)
            {
                $args .= $v['args'][$key];
                if($key < $size)
                {
                    $args .= ', ';
                }
            }
        }

        $traces .= '#' . $k . ' ' 
                 . $v['function']
                 . '('.$args.') called at ['
                 . $v['file'].':'.$v['line'].']';
    }
    else
    {
        $function = (array_key_exists('function',$v)) ? 
                        $v['function'].'() ' : 'function_name';
        $file     = (array_key_exists('file',$v)) ? 
                        $v['file'] : 'file_name';
        $line     = (array_key_exists('line',$v)) ? 
                        $v['line'] : 'line';
        $traces  .= "#{$k} $function called at {$file}:{$line}\n";//This line giving me notice...

    }


}

I am getting notice as Array to string conversion here:

$traces .= "#$k $function called at $file:$line\n";

I actually want to convert this array into string. Is there any method or function which can do the conversion without giving me any notice...

How do I correct this?

1
  • Either $function, $file or $line is an array... Commented Oct 18, 2012 at 16:23

3 Answers 3

1

you begin with:

foreach($traces as $k=>$v) <- $traces here is an array

then you try to do

$traces.= "xxx" <- $traces here is handled as a string

i would rather define a $tracestr string for aggregating text content.

Sign up to request clarification or add additional context in comments.

2 Comments

dweeves is correct.. If my assumption is correct, you didn't mean to use the same variable name for the $traces array and the string that you are building
yes man.. just trying to avoid to many variables.. is it not a good way
0

You are not creating array properly

 $args .= $v['args'][$key];

You are creating a string.

 $args = array(); 
                if(isset($v['args']) && is_array($v['args']))
                {
                    $size = count($v['args']);
                    foreach ($v['
                    args'] as $key => $arg)
                    {
                        array_push($args,$v['args'][$key]);
                       // some of your code
                }

Comments

0
$trace = debug_backtrace();
foreach($traces as ...)

There's something wrong here. $trace is a Debug Backtrace array. While you foreach($traces) ... which seems undefined. And you append to $traces which is supposed to be a non-scalar to foreach it.

Just name your variables properly and make names different!

1 Comment

@troy Why do you append to an array like it was a string $traces .= ...? So where you have $traces .= change to $traces_sz .= and see what happens. And pre-declare it with $traces_sz = ''.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.