Only PHP (Browser-Side)
Hopping out of PHP (Browser-Side)
Of course, one of the easiest things to do (and the most common) is to abstract away the code from the HTML page being loaded, and include it separately in a different file. Speed becomes a factor, as the browser now has to make two GET requests instead of one, but it helps keep code cleaner and more organized.
// You can echo or hop out, I'm gonna hop out // We are assuming the main.js is in the base directory of the site $name = "Ryder"; ?> <script src="/main.js"></script> <?php // Back to PHP echo "we are back in PHP!";
// main.js console.log("Hello, from the other file!")
// main.js // Make sure this file is in the same directory, or adjust the exec path as necessary console.log("Hello, from the Server!")