How can an array be utilized to streamline the process of adding new user roles in PHP code?

When adding new user roles in PHP code, utilizing an array can streamline the process by allowing for easy management and scalability. By storing user roles in an array, you can easily add, remove, or modify roles without having to update multiple parts of your code. This approach also makes it simpler to loop through roles for various operations, such as checking permissions or displaying role options in a form.

// Define an array of user roles
$userRoles = [
    'admin',
    'editor',
    'subscriber',
];

// Loop through user roles
foreach ($userRoles as $role) {
    echo "User role: $role <br>";
}

// Adding a new user role
$userRoles[] = 'contributor';

// Display updated user roles
foreach ($userRoles as $role) {
    echo "User role: $role <br>";
}