What is the DRY principle and how can it be applied to PHP code to avoid repetition?

The DRY (Don't Repeat Yourself) principle is a software development principle that aims to reduce repetition in code. To apply this principle to PHP code, you can create reusable functions or classes to encapsulate common functionality and avoid duplicating code.

// Example of applying the DRY principle in PHP code

// Define a function to calculate the square of a number
function calculateSquare($num) {
    return $num * $num;
}

// Use the calculateSquare function to avoid repeating the calculation
$num1 = 5;
$num2 = 10;

$result1 = calculateSquare($num1);
$result2 = calculateSquare($num2);

echo "Square of $num1 is $result1 <br>";
echo "Square of $num2 is $result2";