PHP 或 (||) 运算符问题
PHP Or (||) Operator Issue
在 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 变量与字符串进行比较时发生错误。在这种情况下,我只关心 'Active' 和 'Pending Resignation' 状态。就好像它正在寻找一个 else 语句来覆盖 'Cancelled' 状态,因为它们应该是之后的第四条 'Active' 记录,并且它在第三次循环迭代时中断。
我能够重现这一点的唯一方法是使用 U+2800 而不是常规 space 字符,这很愚蠢,但你确定你没有使用无效的 space 字符吗?
在 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 变量与字符串进行比较时发生错误。在这种情况下,我只关心 'Active' 和 'Pending Resignation' 状态。就好像它正在寻找一个 else 语句来覆盖 'Cancelled' 状态,因为它们应该是之后的第四条 'Active' 记录,并且它在第三次循环迭代时中断。
我能够重现这一点的唯一方法是使用 U+2800 而不是常规 space 字符,这很愚蠢,但你确定你没有使用无效的 space 字符吗?