为什么将null添加到回显输出?

时间:2019-07-13 16:04:37

标签: php wordpress null echo

将空字符串添加到echo $status输出的末尾:检索到SUCCESSnull,但仅期望SUCCESS

一个Webhook检索XML,目标是发送SUCCESS一词作为不带引号的结果。使用return $status代替echo $status可以使“ SUCCESS”带有引号。

我还看到,当函数不返回任何值时,可能会发生这种情况。但是在这种情况下,无论结果如何,都会强制执行回声。

已经进行了许多小时的更改,结果相同或更差。任何帮助都非常感谢。

function myfunction() {

    //defined as class at the top of the code
    $myobj = new obj ($_REQUEST['messagetype'], $_REQUEST['message']);

    $type = $myobj ->atype;

    if($type === 'MYTYPE'){
        $status = 'SUCCESS';
    } else {
        $status = 'FAIL';
    }
    echo $status;
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'path', '/space', array(
    'methods'  => 'POST',
    'callback' => 'myfunction',
    ));
});

结果 预期:成功或失败(没有引号,空值或其他任何内容)

实际使用echo $status;-> SUCCESSnull或FAILnull

实际为return $status;->“成功”或“失败”

实际为return trim($status, '"');->“成功”或“失败”(仍在引号内)

1 个答案:

答案 0 :(得分:0)

根据文档returning_values,如果函数中省略了return,则添加了“ null”。

就我而言,此问题已通过echo(以避免使用return添加双引号)并在函数末尾包含exit()的方式解决。

感谢您的帮助,希望这对任何人都有用。