How can one optimize PHP code to avoid unnecessary repetition and improve readability?

To optimize PHP code and avoid unnecessary repetition while improving readability, you can use functions to encapsulate reusable logic. By defining functions for common tasks, you can reduce code duplication and make your code more modular and easier to maintain.

// Example of using a function to avoid repetition and improve readability
function calculateArea($length, $width) {
    return $length * $width;
}

$length = 10;
$width = 5;
$area = calculateArea($length, $width);
echo "The area is: " . $area;