Search code examples
phpoperator-keyword

PHP Or (||) Operator Issue


Facing an issue regarding or (double-pipe) operator in PHP.

<?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; 
}
?>

getting an Uncaught Error: Call to undefined function on if statement line within foreach loop. Tried with or and with and without bracketed cases. Single if-case works fine for both comparisons, just not with the double-pipe. Where am I going wrong?

EDIT OUTPUT MESSAGE:

var dump: string(6) "Active" string(6) "Active" string(9) "Cancelled"
error: Fatal error: Uncaught Error: Call to undefined function  () [if line]

The eror occurs on a comparison of $status var against string. In this case, I only care about 'Active' and 'Pending Resignation' status. It's as if it's looking for an else statement to cover the 'Cancelled' status, as their should be a fourth 'Active' record after, and it breaks on third loop iteration.


Solution

  • The only way I was able to reproduce this is by using U+2800 instead of a regular space character, it's silly but are you sure you are not using a invalid space character?

    https://3v4l.org/eL3U8