How can PHP arrays be utilized to store and access user attributes for conditional checks?
To store and access user attributes for conditional checks, you can utilize PHP arrays to store key-value pairs of user attributes. This allows you to easily access and check specific attributes based on keys. You can use conditional statements to check if a specific attribute meets certain criteria.
// Sample user attributes stored in an array
$userAttributes = [
'name' => 'John Doe',
'age' => 30,
'email' => 'john.doe@example.com'
];
// Check if user is over 18 years old
if ($userAttributes['age'] >= 18) {
echo 'User is over 18 years old.';
}
// Check if user's name is John Doe
if ($userAttributes['name'] === 'John Doe') {
echo 'User\'s name is John Doe.';
}