Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if my function print/echo's something?

Tags:

I am often using echo to debug function code:

public function MyFunc() {      // some code...     echo "OK";     // some code...  } 

How can I check that my function print's/echo's something?

(pseudo code):

MyFunc();  if (<when something was printed>){     echo "You forgot to delete echo calls in this function"; } 
like image 895
Grigory Ilizirov Avatar asked Jul 10 '15 09:07

Grigory Ilizirov


2 Answers

This should work for you:

Just call your functions, while you have output buffering on and check if the content then is empty, e.g.

ob_start();  //function calls here MyFunc();  $content = ob_get_contents();  ob_end_clean();  if(!empty($content))     echo "You forgot to delete echos for this function"; 
like image 148
Rizier123 Avatar answered Nov 01 '22 12:11

Rizier123


You could create a $debug flag and a debuglog() function, which checks for the debug flag and only then echos the message. Then you can toggle your debug messages on and off from one location.

define('DEBUGMODE', true); // somewhere high up in a config  function debuglog($msg){     if( DEBUGMODE ){ echo $msg; } } 

Should you ever want to get rid of your debug echos, you can search for "debuglog(" and delete those lines of code. This way you won't accidentally delete any echo statements that are required in normal execution or miss any debug echo statements that should really have been removed.

like image 42
Matt Avatar answered Nov 01 '22 13:11

Matt