Can you provide examples of how to efficiently work with arrays in PHP instead of dynamically creating variables?

When working with arrays in PHP, it is more efficient to store related data in an array rather than dynamically creating individual variables. This allows for easier management and manipulation of data, especially when dealing with a large amount of information. By using arrays, you can iterate through elements, perform operations on the data, and access specific values more easily.

// Example of efficiently working with arrays in PHP

// Creating an array to store related data
$user = [
    'name' => 'John Doe',
    'age' => 30,
    'email' => 'john.doe@example.com'
];

// Accessing values from the array
echo 'Name: ' . $user['name'] . '<br>';
echo 'Age: ' . $user['age'] . '<br>';
echo 'Email: ' . $user['email'] . '<br>';

// Iterating through the array
foreach ($user as $key => $value) {
    echo ucfirst($key) . ': ' . $value . '<br>';
}