How can the code be modified to incorporate CSS classes for styling the elements instead of inline styling?
To incorporate CSS classes for styling the elements instead of inline styling, we can add a class attribute to each HTML element and define the styles in an external CSS file. This helps in separating the content from the presentation, making the code more maintainable and easier to update in the future.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php
$names = array("Alice", "Bob", "Charlie");
echo "<ul>";
foreach ($names as $name) {
echo "<li class='name'>$name</li>";
}
echo "</ul>";
?>
</body>
</html>
```
styles.css:
```css
.name {
color: blue;
font-weight: bold;
}
Related Questions
- What are the advantages and disadvantages of using foreach loops versus for loops when iterating through arrays in PHP to display data in a table?
- What are some best practices for iterating through an array and counting occurrences of a specific value in PHP?
- What role do interfaces play in PHP programming and how can they be effectively utilized in class design?