In what ways can PHP functions be utilized to efficiently display visual indicators like traffic lights based on database values?
To efficiently display visual indicators like traffic lights based on database values in PHP, we can create a function that takes the database value as input and returns the appropriate HTML code for the corresponding traffic light color. We can then call this function wherever we need to display the traffic lights on our webpage.
function displayTrafficLight($status) {
switch ($status) {
case 'red':
return '<div style="background-color: red; width: 50px; height: 50px;"></div>';
break;
case 'yellow':
return '<div style="background-color: yellow; width: 50px; height: 50px;"></div>';
break;
case 'green':
return '<div style="background-color: green; width: 50px; height: 50px;"></div>';
break;
default:
return 'Invalid status';
}
}
// Example usage
$status = 'red';
echo displayTrafficLight($status);