php - Equal string comparisons are failing -
after hours of debugging, have determine reason on particular web-server running small script, 2 equal strings apparently being interpreted not equal each other.
it makes absolutely no sense me have tried comparing using "==" , "strcmp". while code block works on hosting, person running script continues have fail.
the results of logging is...
data before explode: result=success;
data after explode: result=success
no
$ch = curl_init(); curl_setopt($ch, curlopt_url, $globals["api_url"]); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_timeout, 100); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_postfields, $postfields); $data = curl_exec($ch); curl_close($ch); $data1_log = "data before explode: " . $data; log_activity($data1_log); $data = explode(";",$data); $data2_log = "data after explode: " . $data[0]; log_activity($data2_log); if (strcmp($data[0], "result=success") == 0) { log_activity("yes"); } else { log_activity("no"); }
use trim() (note second parameter, accepts other chracters ";" in case) , strtolower()
or strtoupper()
on both strings before comparision.
also, if have option change api response, try implementing json response instead of returning plain string. less memory intensive , more maintainable.
Comments
Post a Comment