有一个功能:
function keyAndNatExistsForTheExercice($nat, $exercice, $hint, $pk) {
$critere = array();
$critere['nat_impot_code'] = ($nat == "" ? "IS NULL" : $nat);
$critere['exer_annee'] = $exercice;
$critere['param_exer_hint'] = $hint;
$enreg = $this->val_param_exercice->lireParCritere($critere);
if ($enreg['cnt'] > 0)
// I want to set value to the $pk param here
return ($enreg['cnt'] == 0 ? false : true);
}
正如您所看到的,我想在parameter
内为function
设置一个值,我想在其他地方使用该值。这可能吗?
答案 0 :(得分:3)
您可以通过引用传递变量,例如:
<?php
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
// $a is 6 here
?>
来自PHP manual。
答案 1 :(得分:1)
您可以返回一个数组,其中包含'cnt'的true或false值以及您想要$ pk的值。
$array['returnValue'] = ($enreg['cnt'] == 0 ? false : true);
$array['pk'] = //whatever you want to give it here.
return $array;
然后您只需要更改参数的值。
否则你通过引用给出$ pk参数,这样它就会给出值的变化。
function keyAndNatExistsForTheExercice($nat, $exercice, $hint, &$pk) {
$pk = //some Value
}
或者您将值保存到会话中并使用保存在那里的值。
答案 2 :(得分:1)
您可以使用对该变量的引用。 E.g:
$nat = 5;
$exercice = true; // Typo?
$hint = 'Hint';
$pk = 1;
keyAndNatExistsForTheExercice($nat, $exercice, $hint, $pk);
注意强>
您无法直接将参考值传递给函数:
keyAndNatExistsForTheExercice($nat, true, $hint, $pk); // Will throw an error as &$exercice must be variable.
function keyAndNatExistsForTheExercice($nat, &$exercice, $hint, $pk) {
/* ... */
}
另请注意,您也可以从函数中返回多个值:
list[$a, $b] = myFunction();
function myFunction() {
return array('value Of A', 'value Of B');
}
答案 3 :(得分:0)
您可以将其存储到$ _SESSION []变量中,因此只要用户所在,就会存储此值(服务器端)。 但是,您需要让会话已经运行。