1

I have 2 functions that do the exact same thing but for different variables:

function checkRights($user){
    if($user['rights'] == 'DEVELOPER'){
        $GLOBALS['crowncol'] = "#9A9A9A";
        echo' <img src="/Developer.png" alt="Developer Icon"/>';
    } else if($user['rights'] == 'ADMINISTRATOR'){
        $GLOBALS['crowncol'] = "#D0CE2C";
        echo' <img src="/admin.png" alt="Administrator Icon"/>';
    } else if($user['rights'] == 'MODERATOR'){
        $GLOBALS['crowncol'] = "#FFCC00";
        echo' <img src="mod.png" alt="Moderator Icon"/>';
    } else if($user['rights'] == 'DONATOR'){
        $GLOBALS['crowncol'] = "#FF0000";
        echo' <img src="donator.png" alt="Donator Icon"/>';
    } else if($user['rights'] == 'SUPER_DONATOR'){
        $GLOBALS['crowncol'] = "#9A9A9A";
        echo' <img src="/superdonator.png" alt="Super Donator Icon"/>';
    } else if($user['rights'] == 'LEGENDARY_DONATOR'){
        $GLOBALS['crowncol'] = "#9A9A9A";
        echo' <img src="/respecteddonator.png" alt="Legendary Donator Icon"/>';
    } else if($user['rights'] == 'PLAYER'){
        $GLOBALS['crowncol'] = "#037AAC";
    }
}
function checkPlayerRights($player){
    if($player['rights'] == 'DEVELOPER'){
        $GLOBALS['crowncol'] = "#9A9A9A";
        echo' <img src="/Developer.png" alt="Developer Icon"/>';
    } else if($player['rights'] == 'ADMINISTRATOR'){
        $GLOBALS['crowncol'] = "#D0CE2C";
        echo' <img src="/admin.png" alt="Administrator Icon"/>';
    } else if($player['rights'] == 'MODERATOR'){
        $GLOBALS['crowncol'] = "#FFCC00";
        echo' <img src="/mod.png" alt="Moderator Icon"/>';
    } else if($player['rights'] == 'DONATOR'){
        $GLOBALS['crowncol'] = "#FF0000";
        echo' <img src="/donator.png" alt="Donator Icon"/>';
    } else if($player['rights'] == 'SUPER_DONATOR'){
        $GLOBALS['crowncol'] = "#9A9A9A";
        echo' <img src="/superdonator.png" alt="Super Donator Icon"/>';
    } else if($player['rights'] == 'LEGENDARY_DONATOR'){
        $GLOBALS['crowncol'] = "#9A9A9A";
        echo' <img src="/respecteddonator.png" alt="Legendary Donator Icon"/>';
    } else if($player['rights'] == 'PLAYER'){
        $GLOBALS['crowncol'] = "#037AAC";
    }
}

I was wondering if there is a way I can combine them both into a function called checkRights so that all I have to do is execute one function but call which variable I want it to run the check.

For example: checkRights($user); or checkRights($player);

3
  • Yes, that is how functions work, pass it whatever you need. The variable inside the function has its own scope, you can call it what every you want in the definition. Also look at SWITCH statements, that could be easier to read. Commented Aug 2, 2018 at 16:29
  • 1
    Yes you can :-) and are already done as far as i can see. just remove the checkPlayerRights function. Commented Aug 2, 2018 at 16:29
  • They are the same function. The variable name doesn't make a difference. Commented Aug 2, 2018 at 16:37

1 Answer 1

2

Build a single function that takes one argument (which you had already done anyway):

function checkRights($obj){
    if($obj['rights'] == 'DEVELOPER'){
        $GLOBALS['crowncol'] = "#9A9A9A";
        echo' <img src="/Developer.png" alt="Developer Icon"/>';
    } 
    // ...
    else if($obj['rights'] == 'PLAYER'){
        $GLOBALS['crowncol'] = "#037AAC";
    }
}

Now call the function like so:

checkRights($player);
checkRights($user);
Sign up to request clarification or add additional context in comments.

1 Comment

$user works fine. Code doesn't need to change at all. Just change the function call and OP is good to go..

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.