22

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";
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Grigory Ilizirov
  • 1,030
  • 1
  • 8
  • 26
  • 4
    unclear what you asking. – Shaiful Islam Jul 10 '15 at 09:39
  • 6
    Since it's __your__ function, you should know what's inside – Alma Do Jul 10 '15 at 09:54
  • Write your tests instead of debugging via echo statements. – Łukasz Rogalski Jul 10 '15 at 10:41
  • 2
    @AlmaDo people can be forgetful - this is a great way to clean up. – Seiyria Jul 10 '15 at 12:43
  • 1
    @Seiyria well, if function is so long that at first look you can't get if it echoes something or not - then it's a way too long function. I would not make my method longer than ~12 lines (including prototype) – Alma Do Jul 10 '15 at 12:50
  • 2
    @AlmaDo it's not a matter of the function being long; the codebase could just be large. I forget that I have `console.log` in my code occasionally -- I have CI to catch that error, because sometimes you just forget. – Seiyria Jul 10 '15 at 13:07
  • I might recommend writing a debug print function that can be access anywhere in your code. Have a flag to turn it on or off, and when you need to get rid of all of the debug code - you simply have to search for it (rather then the generic print statements). For even more usability - you could even attach logs to it to print logs every time the function is called, including where it was called from. – Sh4d0wsPlyr Jul 10 '15 at 19:48

6 Answers6

36

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";
JCasso
  • 5,423
  • 2
  • 28
  • 42
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • 1
    As an alternative, you could also check if the headers have been sent: http://php.net/manual/en/function.headers-sent.php – jeroen Jul 10 '15 at 09:58
  • 1
    @jeroen Ah yeah, that's also a way, didn't thought about that :) (Well it maybe gives you some trouble when you already echo'ed something above the function call) – Rizier123 Jul 10 '15 at 10:00
  • Just be careful if the tested function itself also plays with the output buffer. – Guillaume Pagnard Jul 10 '15 at 10:40
  • Be absolutely sure you know what you're doing with the output buffer. Thinkgs might act weird when you get more advanced. I've used outputbuffer only once in the last few years. – Martijn Jul 10 '15 at 12:59
  • You can abbreviate that using [ob_get_clean()](http://php.net/manual/en/function.ob-get-clean.php). – deltab Jul 10 '15 at 19:57
16

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.

Martijn
  • 15,791
  • 4
  • 36
  • 68
Matt
  • 1,377
  • 2
  • 13
  • 26
  • Sounds good, but eventualy i want to remove all the debuglog calls from my code.... – Grigory Ilizirov Jul 10 '15 at 10:03
  • 1
    At which point you could just do a search for all instances of `"debuglog("` and delete those lines of code. – Matt Jul 10 '15 at 10:05
  • 5
    The most adequate solution, IMHO –  Jul 10 '15 at 10:20
  • @GrigoryIlizirov If you search-and-remove debugging statements from your code then you are doing it wrong! – Salman A Jul 10 '15 at 11:12
  • I've improved this a bit. A contant can never change value, is a bit safer. A constant is defined globally, no need to global variables, is a bit safer (`example.com/?debug=true` could break it) – Martijn Jul 10 '15 at 13:02
  • @Martijn `example.com/?debug=true could break it` --- Only if I was using `$_GET['debug']`. Unless there's something I'm missing? – Matt Jul 10 '15 at 13:17
  • I highly approve of this solution. I might even go a step further and add a log/tracker to it though. For instance if you have a log in the database, throw some database logs so you can track it in the database as well. Only works well with actual debug code though - less so for random programmer comments like "Ok its here" or "Xth iteration" – Sh4d0wsPlyr Jul 10 '15 at 19:51
2

It's the bad way checking if something is echoed.

You can set a variable named is_echoed to 1 or you can return the value

public $is_echoed = 0;
//rest
$this->is_echoed = 1;

or

function myFunc()
{
  return "OK";
}
if(myFunc() == 'OK')
     //rest
Mahdyfo
  • 1,155
  • 7
  • 18
1

You can use var_dump() and die() to debug your code more efficiently.

$test = "debud test";
public function MyFunc($test)
{
// some code...
var_dump($test); die();
// some code...
}

Reference: http://php.net/manual/en/function.var-dump.php

http://php.net/manual/en/function.die.php

Filipe Ferreira
  • 320
  • 2
  • 21
1

Why do you want to try such an extensive process of seeing if something has been echoed or not?

For debugging you can definitely use echo to see if the particular block is being hit during a particular use-case. But I would suggest you use flags and return the values to the calling function.

function xyz () {

     if (something) return some_value;
     else return some_other_value;
}

There is no particular need to have variables and use space in storing a 0 or 1 when you can just return a hard-coded literal.

adlawson
  • 6,303
  • 1
  • 35
  • 46
Soumojit Ghosh
  • 921
  • 7
  • 16
0

I would suggest to you to use something like log4php [1]

But if not, I use a function like this:

define('DEBUG', true);
function debug($msg){
    if(DEBUG){ echo $msg; }
}

Or something like this to see the log in the browser console:

function debug_to_console( $data ) {

    if ( is_array( $data ) )
        $output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
    else
        $output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";

    echo $output;
}
Gonz
  • 1,198
  • 12
  • 26