变量中的变量转储


Var Dump in variable

我在PHP中有一个API。如果您有任何错误,它返回 true,如果没有,则返回 false。我像这样捕捉到它:

$ch = curl_init("http://localhost/api/v1/register");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);

我可以这样打印它:

var_dump($response);

现在,在测试中,它返回了以下内容:

字符串(4) "真"

例如,如何捕获变量上的真或假以用于 if?

从 Rizier123 继续,看起来$response包含字符串 true 而不是布尔值 true。我相信PHP也认为包含单词false的字符串在布尔意义上是"true",因为它不是空的或值为0。

所以也许只是这样做:

$responseValue = ($response === 'true') ? true : false;

然后在你的 if 语句中使用$responseValue