In what situations should code be directly integrated into a page versus being encapsulated within a function in PHP, and how does this impact server performance?

Code should be directly integrated into a page when it is specific to that page and not reusable elsewhere. On the other hand, code should be encapsulated within a function when it is reusable across multiple pages or when it performs a specific task that can be abstracted. Directly integrating code can impact server performance by increasing the size of the page and potentially making it harder to maintain, while using functions can improve performance by promoting code reuse and organization.

// Directly integrating code into a page
<?php
// Code specific to this page
echo "Hello, World!";
?>

// Encapsulating code within a function
<?php
function sayHello() {
    echo "Hello, World!";
}

// Call the function
sayHello();
?>