在这个基本代码中,我检查特定用户和国家是否存在具有特定itemId的记录。函数外部的值与函数内部的值不同
该函数内部的记录返回1
,但是当它在外面时,它是完全不同的。
function recordExists(&$d) {
global $conn, $userId, $userCountryCode;
$sqlFindRecord = "select count(itemId) as recordCount from onr_items_mod where itemTempId = :itemTempId and itemStatus IN(0,5,8) and itemPublishedStatus = 2 and itemUserId = :userId and itemUserCountryCode = :userCountryCode";
$countRecord = $conn->prepare($sqlFindRecord);
$countRecord->execute(array(
":itemTempId" => $d,
":userId" => $userId,
":userCountryCode" => $userCountryCode
));
$countRecord->bindColumn('recordCount',$d);
echo 'This is D '.$d.' ---';
$row = $countRecord->fetch();
if($d == 0) {
return $d;
} elseif ($d == 1) {
return $d;
} else {
return false;
}
}
$d = 'US01'
if(recordExists($d) == 0) { //If the function returned 0
echo $d;
echo 'Was Zero';
} elseif (recordExists($d) == 1) { //If the function returned 1
echo $d;
echo 'Was One';
} else {
echo $d;
echo 'Neither Zero or One';
}
在我完成上述操作后,它总是'不是零或一个'。为什么是这样?我尝试了整个早晨,但是却无法想出解决方法。
这工作,但不是上述
function counter(&$a) {
if($a == 0) {
return $a;
} elseif ($a == 1) {
return $a;
} else {
return false;
}
}
$a = 1;
if(counter($a) == 0) {
echo $a;
} elseIf (counter($a) == 1) {
echo $a;
} else {
echo 'Umm...'.$a;
}
为什么你通过引用传递'$ d'到函数? –
因为'$ d'的值在函数内部发生了变化。 – Norman
如果你改变它并且按值传递,那么只需要使用函数的返回值就会更简单。 –