How can PHP code be optimized to avoid repetitive if statements?

To avoid repetitive if statements in PHP code, you can use a switch statement instead. Switch statements are more efficient and cleaner than multiple if statements, especially when dealing with multiple conditions. By using a switch statement, you can easily handle different cases without nesting if statements.

// Example of using a switch statement to avoid repetitive if statements
$color = "red";

switch ($color) {
    case "red":
        echo "The color is red.";
        break;
    case "blue":
        echo "The color is blue.";
        break;
    case "green":
        echo "The color is green.";
        break;
    default:
        echo "Unknown color.";
}