简体   繁体   English

如何检查我的功能是否打印/回声?

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

I am often using echo to debug function code: 我经常使用echo来调试功能代码:

public function MyFunc() {

    // some code...
    echo "OK";
    // some code...

}

How can I check that my function print's/echo's something? 如何检查我的函数print / echo是什么?

(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, eg 只需调用您的函数,同时启用输出缓冲并检查内容是否为空,例如

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. 您可以创建一个$debug标志和一个debuglog()函数,它检查调试标志,然后只回显消息。 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. 如果您想要摆脱调试回声,可以搜索"debuglog("并删除那些代码行。这样您就不会意外删除正常执行​​中所需的任何echo语句或错过任何调试回声语句应该真的被删除。

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 您可以将名为is_echoed的变量设置为1,也可以返回该值

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

or 要么

function myFunc()
{
  return "OK";
}
if(myFunc() == 'OK')
     //rest

You can use var_dump() and die() to debug your code more efficiently. 您可以使用var_dump()die()来更有效地调试代码。

$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.var-dump.php

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

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. 对于调试,您肯定可以使用echo来查看在特定用例期间是否正在命中特定块。 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. 当你只能返回一个硬编码的文字时,没有特别需要变量和使用空间来存储0或1。

I would suggest to you to use something like log4php [1] 我建议你使用像log4php这样的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM