1

Below, I have a function call that gives one of the list items the active class (from Bootstrap) if the current filename matches the first argument of the giveClassActive(). Each list item has text it will show, an href for the page to direct to, and a filename that it is checking for. Only sometimes are the $href and $file values different because # must become %23. So, I want to give the $href a default value of $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'); 
?>

My question is: How do I reference another parameter in a function definition?

I have tried

function giveClassActive($file, $href = $file, $show) {

and

function giveClassActive($file, $href = this.$file, $show) {

which produce

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

Note: The output is

Output

Edit after put on hold

My question is different from PHP function with variable as default value for a parameter because that question does not ask about referencing a parameter, but rather another variable.

1
  • 1
    note about dupes: It's not necessarily the same question - but the same solution that makes it a dupe Commented Jan 7, 2019 at 9:52

1 Answer 1

0

Best is

if ($href == null)
    $href = $file;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.