簡體   English   中英

將變量傳遞給php中的函數參數

[英]Pass variable to a function parameter in php

我有以下代碼,我嘗試將名為“$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 yearDropdown($startYear, $endYear, $id="yearOfBirth", $match){
   //function code here...
}

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

您不能在函數聲明中將默認值設置為變量。

刪除$match =並更新您的函數以使用$tutorDetailYearOfBirth而不是$match

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM