【发布时间】:2021-03-21 14:03:09
【问题描述】:
在 PHP 中遇到关于 or(双管道)运算符的问题。
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$token = fetchToken();
fetchActiveCompanies($token);
function fetchActiveCompanies($token){
//GET THE COMPANIES
$ch = curl_init();
$vars = 'Id, Member_Status__c, Name';
$query = 'https://xxx.my.salesforce.com/services/data/v50.0/query?q=SELECT ' . $vars . ' FROM Account';
$query = str_replace (' ', '%20', $query);
curl_setopt_array($ch, array(
CURLOPT_URL => $query,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $token
),
));
$response = curl_exec($ch);
curl_close ($ch);
$obj = json_decode($response);
$records = $obj->records;
$activeRecords = array();
foreach($records as $record){
$id = $record->Id;
$status = $record->Member_Status__c;
var_dump($status);
if (($status == "Active") || ($status == "Pending Resignation")){
//if ($status == 'Active'){
$object = array();
$object['name'] = $record->Name;
$activeRecords[] = $object;
}
}
echo("record is " . json_encode($activeRecords));
return json_encode($activeRecords);
}
function fetchToken(){
//returns token string;
}
?>
在 foreach 循环中的 if 语句行上获得 Uncaught Error: Call to undefined function。尝试使用or 以及带和不带括号的情况。单个 if-case 对两种比较都适用,只是不适用于双管。我哪里错了?
编辑输出消息:
var dump: string(6) "Active" string(6) "Active" string(9) "Cancelled"
error: Fatal error: Uncaught Error: Call to undefined function () [if line]
错误发生在 $status var 与字符串的比较中。在这种情况下,我只关心“Active”和“Pending Resignation”状态。就好像它正在寻找一个覆盖“已取消”状态的 else 语句,因为它们应该是之后的第四个“活动”记录,并且它在第三次循环迭代时中断。
【问题讨论】:
-
你试过去掉括号吗?
-
这段代码 sn-p 不调用函数。错误发生在哪一行?
-
@JohnnyRockex 请edit 您的问题包含您作为minimal reproducible example 的完整源代码,其他人可以对其进行测试。还包括您收到的关于您发布的 MCVE 的完整完整错误消息。
-
@JohnnyRockex 您发布的代码仍然不完整。错误消息也不完整。请edit 您的问题包括 MCVE 和完整的错误消息。出于测试目的,请在
if()语句之前添加var_dump($status);以查看$status变量的内容。 -
我能够重现此问题的唯一方法是使用 U+2800 而不是常规空格字符,这很愚蠢,但您确定您没有使用无效的空格字符吗? 3v4l.org/eL3U8
标签: php operator-keyword