How can proper code indentation and formatting improve the readability and maintainability of PHP scripts, especially when working with complex queries or functions?
Proper code indentation and formatting in PHP scripts can greatly improve readability and maintainability, especially when dealing with complex queries or functions. By consistently indenting code blocks, using clear and descriptive variable names, and following a consistent coding style, developers can easily understand the logic of the script and make changes or updates more efficiently.
// Example of properly formatted and indented PHP code
$query = "SELECT * FROM users WHERE age > 18";
$result = mysqli_query($connection, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row['name'] . "<br>";
echo "Age: " . $row['age'] . "<br>";
}
} else {
echo "Error executing query: " . mysqli_error($connection);
}