Skip to content
PHP Logo

Are there any coding standards or guidelines for formatting code, such as indentation, when using foreach loops in PHP?

When using foreach loops in PHP, it is important to follow coding standards and guidelines for formatting code to ensure readability and maintainability. One common practice is to use consistent indentation, typically four spaces, within the foreach loop block. This helps improve code clarity and makes it easier for other developers to understand and work with the code.

php
// Example of foreach loop with proper indentation
$colors = array("red", "green", "blue");

foreach ($colors as $color) {
    echo $color . "<br>";
}