How can PHP arrays be effectively utilized to optimize code and reduce redundancy?
To optimize code and reduce redundancy using PHP arrays, you can store repetitive data in an array and access it dynamically throughout your code. This helps in centralizing data, making it easier to update and maintain. Additionally, using arrays can simplify loops and conditional statements by iterating over array elements rather than repeating the same code multiple times.
// Storing repetitive data in an array
$userData = [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'age' => 30
];
// Accessing array elements dynamically
echo "Name: " . $userData['name'] . "<br>";
echo "Email: " . $userData['email'] . "<br>";
echo "Age: " . $userData['age'] . "<br>";