[英]How do you reference another parameter as the default value of a parameter in a function?
下面,我有一個函數調用,如果當前文件名與giveClassActive()
的第一個參數匹配,則為列表項之一提供active
類(來自Bootstrap giveClassActive()
。 每個列表項都有將顯示的文本,指向頁面的href和要檢查的文件名。 $href
和$file
值僅有時不同,因為#
必須變為%23
。 因此,我想給$href
一個默認值$file
。
<?php
function giveClassActive($file, $href, $show) {
echo '<li class = "';
$filename = explode('/', $_SERVER['SCRIPT_NAME']);
$file_name = end($filename);
if ($file_name == $file) {
echo 'active';
}
echo '">';
echo '<a href = "' . $href . '">' . $show . '</a></li>';
}
giveClassActive('calculator #5.php', 'calculator %235.php', 'Calculator');
giveClassActive('contact.php', 'contact.php', 'Contact');
?>
我的問題是:如何在函數定義中引用另一個參數?
我努力了
function giveClassActive($file, $href = $file, $show) {
和
function giveClassActive($file, $href = this.$file, $show) {
產生
Fatal error: Constant expression contains invalid operations in C:\\Bitnami\\wampstack-7.1.22-1\\apache2\\htdocs\\LarryUllman\\Chapter 3\\includes\\header.html on line 20
注意:輸出為
暫停后編輯
我的問題與PHP函數的不同之處在於, PHP函數將變量作為參數的默認值,因為該問題不是詢問引用參數,而是詢問另一個變量。
最好的是
if ($href == null)
$href = $file;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.