How can the function getRights be modified to return the desired value instead of an array?
The issue can be solved by modifying the getRights function to return the desired value directly instead of returning an array. This can be done by accessing the specific value within the array and returning it. By making this change, the function will directly return the desired value without needing to access it from the array.
function getRights($user) {
$rights = [
'admin' => 1,
'editor' => 2,
'viewer' => 3
];
if (isset($rights[$user])) {
return $rights[$user];
}
return null;
}
// Example usage
$user = 'admin';
$rights = getRights($user);
echo $rights; // Output: 1
Keywords
Related Questions
- What potential security risks are associated with storing login information in a PHP file?
- How can one address the fatal error "Type of xml_format_exception::$line must be int (as in class Exception)" in PHP?
- What are the differences between include() and require() in PHP, and how do they impact script execution?