![](/img/trans.png)
[英]How can I echo something only if the woo-commerce product id is listed in my array?
[英]How can I check if my function print/echo's something?
我经常使用echo来调试功能代码:
public function MyFunc() {
// some code...
echo "OK";
// some code...
}
如何检查我的函数print / echo是什么?
(伪代码):
MyFunc();
if (<when something was printed>){
echo "You forgot to delete echo calls in this function";
}
这应该适合你:
只需调用您的函数,同时启用输出缓冲并检查内容是否为空,例如
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";
您可以创建一个$debug
标志和一个debuglog()
函数,它检查调试标志,然后只回显消息。 然后,您可以从一个位置打开和关闭调试消息。
define('DEBUGMODE', true); // somewhere high up in a config
function debuglog($msg){
if( DEBUGMODE ){ echo $msg; }
}
如果您想要摆脱调试回声,可以搜索"debuglog("
并删除那些代码行。这样您就不会意外删除正常执行中所需的任何echo语句或错过任何调试回声语句应该真的被删除。
检查某些事情是否得到回应是不好的方法。
您可以将名为is_echoed的变量设置为1,也可以返回该值
public $is_echoed = 0;
//rest
$this->is_echoed = 1;
要么
function myFunc()
{
return "OK";
}
if(myFunc() == 'OK')
//rest
您可以使用var_dump()
和die()
来更有效地调试代码。
$test = "debud test";
public function MyFunc($test)
{
// some code...
var_dump($test); die();
// some code...
}
为什么你想尝试这样一个广泛的过程,看看是否有回声?
对于调试,您肯定可以使用echo来查看在特定用例期间是否正在命中特定块。 但我建议你使用标志并将值返回给调用函数。
function xyz () {
if (something) return some_value;
else return some_other_value;
}
当你只能返回一个硬编码的文字时,没有特别需要变量和使用空间来存储0或1。
我建议你使用像log4php
这样的log4php
[1]
但如果没有,我使用这样的函数:
define('DEBUG', true);
function debug($msg){
if(DEBUG){ echo $msg; }
}
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.