What are some best practices for organizing PHP code to avoid repetition and improve maintainability?

To avoid repetition and improve maintainability in PHP code, it is recommended to follow the DRY (Don't Repeat Yourself) principle and use functions, classes, and namespaces effectively. By organizing code into reusable functions and classes, you can reduce redundancy and make it easier to update and maintain the code in the future.

// Example of organizing PHP code using functions and classes

// Define a function to calculate the area of a circle
function calculateCircleArea($radius) {
    return pi() * $radius * $radius;
}

// Define a class to represent a person
class Person {
    public $name;
    public $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function greet() {
        return "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
    }
}