What are some best practices for organizing PHP code to avoid issues with variable scope and definition?

To avoid issues with variable scope and definition in PHP, it is best practice to define variables within the appropriate scope, such as within functions or classes where they are needed. Avoid using global variables whenever possible to prevent conflicts and unintended side effects. Additionally, use naming conventions that clearly indicate the purpose and scope of variables to make code more readable and maintainable.

function calculateTotal($items) {
    $total = 0; // Define the variable within the function scope
    foreach ($items as $item) {
        $total += $item;
    }
    return $total;
}