user2583501
user2583501

Reputation: 33

Find where PHP script outputs a string

When I run php someScript.php I get some expected output and some unexpected strings. They look like debugging strings and were probably placed there during development. The file is a part of a larger framework and can include other files that can further require others.

I have no idea which function (echo, print, var_dump, write, ...) is being used to output the (unwanted) strings.

How can I find a file & row number of a place where a (dynamically generated) string is being outputted to stdout.

thx!

Upvotes: 2

Views: 389

Answers (2)

likle
likle

Reputation: 1797

It's a bit ugly but you could try to mess with the output buffers. The following approach seems to work for me:

<?php
$locations = [];
ob_start(function($buffer) {
  global $locations;
  $locations[] = debug_backtrace();
  return $buffer;
}, 1);

// Your code with output here.

// $locations should contain the information now
print_r($locations); 
?>

Upvotes: 2

Jessica
Jessica

Reputation: 7005

You should be able to tell from the structure of the output if it's var_dump, dd, or if it's a more basic echo. If you search the codebase for echo, are there actually that many of them?

You can try using xdebug to start stepping through the code line by line, and figure out where it's being printed. If the string being printed truly has no static text you can grep the codebase for, this is where I'd start.

Upvotes: 0

Related Questions