What are the best practices for passing variables between PHP functions and HTML elements?

When passing variables between PHP functions and HTML elements, it is best practice to use PHP to echo out the variable values directly into the HTML elements. This can be done by assigning the variable value to a PHP variable and then echoing it within the HTML code. This ensures that the variables are securely passed and displayed in the HTML elements.

<?php
// PHP function to retrieve variable value
function getVariableValue() {
    $variable = "Hello World!";
    return $variable;
}

// Assign the variable value to a PHP variable
$variableValue = getVariableValue();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Passing Variables</title>
</head>
<body>
    <h1><?php echo $variableValue; ?></h1>
</body>
</html>