What are some best practices for incorporating JavaScript functions within PHP code?

When incorporating JavaScript functions within PHP code, it is important to remember that PHP is server-side and JavaScript is client-side. To ensure smooth integration, you can use PHP to generate JavaScript code dynamically. This can be achieved by echoing JavaScript code within PHP using the `echo` or `print` statements.

<?php
// PHP code
$data = "Hello, World!";

// JavaScript function within PHP
echo "<script>";
echo "function displayData(data) {";
echo "   alert(data);";
echo "}";
echo "</script>";
?>