在php中将变量传递给函数参数

在php中将变量传递给函数参数,php,function,Php,Function,我在下面的代码中尝试将名为“$tutorDetailYearOfBirth”的变量传递到函数中 但是,函数keep不会检测$tutorDetailYearOfBirth的值 function yearDropdown($startYear, $endYear, $id="yearOfBirth", $match=$tutorDetailYearOfBirth) { echo "<select id=".$id." name=".$id." class='form-control'

我在下面的代码中尝试将名为“$tutorDetailYearOfBirth”的变量传递到函数中

但是,函数keep不会检测$tutorDetailYearOfBirth的值

function yearDropdown($startYear, $endYear, $id="yearOfBirth", $match=$tutorDetailYearOfBirth)
{ 
    echo "<select id=".$id." name=".$id." class='form-control'>n"; 
    for ($i=$startYear;$i<=$endYear;$i++){
        //echo "<option value=".$i.">".$i."</option>n"; 
       echo '<option value="'.$i.'" '.(($i==$match)?'selected="selected"':"").'>'.$i.'</option>';
    }
    echo "</select>"; 
}
function year下拉列表($startYear、$endYear、$id=“yearOfBirth”,$match=$tutorDetailYearOfBirth)
{ 
回声“n”;

对于($i=$startYear;$i),不能在函数声明中将默认值设置为变量

删除
$match=
并更新函数以使用
$tutorDetailYearOfBirth
而不是
$match

function yearDropdown($startYear, $endYear, $id="yearOfBirth", $tutorDetailYearOfBirth){ 
....

定义函数变量的默认值时不能使用变量。您需要将其更改为:

function yearDropdown($startYear, $endYear, $id="yearOfBirth", $match){
   //function code here...
}

yearDropdown($startYear, $endYear, $id, $tutorDetailYearOfBirth);

不能将函数参数的默认值设置为变量,只能将其设置为从-
引用的静态值。默认值必须是常量表达式,而不是(例如)变量、类成员或函数调用。
我很好奇您计划如何使用它。更改函数中的变量名会有什么帮助?这会破坏他的函数。您错过了
$id
也在做同样的事情。@AlanMachado
$id
被设置为默认值-一个字符串。@Styphon在很明显,如果参数上的变量名发生变化,代码中的变量将随着well@MaggsWeb如果我在发布后添加详细信息并不重要,这不会使您的答案错误减少。@MaggsWeb您的意思是什么?编辑您的问题/答案没有错werI没有看到这两个答案之间的区别,@AlanMachado没有区别-除了变量名(只要它与传递给函数的变量匹配,就没有区别)@AlanMachado他们非常相似,但其中一个只是更改了变量名。答案在一分钟后关闭,所以可能他们有相同的思维过程