What is the purpose of the function display_data() in the PHP code provided?
The purpose of the function display_data() in the PHP code provided is to output the data stored in the $data variable in a formatted manner. The function loops through the array and echos out each key-value pair. To fix the issue and ensure that the data is displayed correctly, we need to properly define the $data variable before calling the display_data() function.
<?php
$data = array(
"name" => "John Doe",
"age" => 30,
"email" => "johndoe@example.com"
);
function display_data($data) {
foreach ($data as $key => $value) {
echo $key . ": " . $value . "<br>";
}
}
// Call the function to display the data
display_data($data);
?>
Keywords
Related Questions
- How can a PHP developer differentiate between logged-in and non-logged-in users on a website?
- In what situations might line breaks disappear when copying text to a MySQL database and then retrieving it, and how can this issue be addressed in PHP?
- How important is it for PHP developers to maintain a consistent naming convention for variables, functions, and database queries to avoid confusion and errors in their code?