PHP在字符串末尾插入null

时间:2017-08-18 21:43:44

标签: php

我一直在为我的网站编写简单的api,当函数显示字符串时,它会在

结尾处附加null
<?php
//error_reporting(0);

class EnterData {

function addPlayedGames ($games) {
    echo $games;
}

}

switch ($_GET['method']) {
    case 'say':
            $response = 'Hello';
        break;
    case '':
            $response = 'No method has been called';
        break;
    case 'addToPlayCount':
            $enter_data = new EnterData();
            $response = $enter_data->addPlayedGames($_GET['games']);
        break;

    default:
            $response = 'Unknown method | Error 01';
        break;
}


if(!isset($_GET['method'])){
    $response = 'Nothing has been passed.';
}

@header('Content-Type: application/json; charset=utf8');
echo json_encode($response);

?>

E.g。

  

examle.com/api.php?method=addToPlayCount&games=31293123

显示器

  

31293123null

1 个答案:

答案 0 :(得分:6)

此功能不会返回任何内容。

function addPlayedGames ($games) {
    echo $games;
}

Functions that don't explicitly return a value will implicitly return null.

所以当你在这里使用那个功能时:

$response = $enter_data->addPlayedGames($_GET['games']);

$response设置为nulljson_encode(null)返回字符串&#34; null&#34;。