What are the best practices for formatting and organizing PHP code to avoid errors?

When formatting and organizing PHP code, it is important to follow best practices to avoid errors and make the code more readable. Some key practices include using consistent indentation, following naming conventions, organizing code into logical sections, and commenting code effectively.

<?php

// Example of well-formatted and organized PHP code
class User {
    private $username;
    private $email;

    public function __construct($username, $email) {
        $this->username = $username;
        $this->email = $email;
    }

    public function getUsername() {
        return $this->username;
    }

    public function getEmail() {
        return $this->email;
    }
}

$user = new User('john_doe', 'john.doe@example.com');
echo $user->getUsername();
echo $user->getEmail();

?>