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"; }
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";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With