A Guide to Executing JavaScript with PHP

If you’re looking to execute JavaScript with PHP, you’ve got a few options. In almost all cases, you should provide the JavaScript code to the user’s browser as a part of the page, which can be done in a few ways.


Only PHP (Browser-Side)

This can be executed fairly easy only using PHP. The PHP code “echos” the script tag and the JavaScript code within it into the HTML DOM. The user’s browser, when loading the page, then finds and executes this code.

 // Only using PHP
echo (
     '<script type="text/javascript">',
          'console.log("Hello World!");',


Hopping out of PHP (Browser-Side)

Another option to include JavaScript is to simply hop out of PHP and write it. This option plays the best with most text editors, as it allows for better highlighting. It can however in large cases make the code less clean, but even then, I generally prefer this method.

 // Hopping out of PHP
$name = "Ryder";
     '<script type="text/javascript">',
          'console.log("Hello <?php echo $name; ?>!");',


Including JavaScript From Another File (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>
// Back to PHP
echo "we are back in PHP!";


// main.js
console.log("Hello, from the other file!")


Executing JavaScript Server Side

This one can be inherently dangerous, so it’s important it’s done with a good amount of caution. I’ve previously had projects that have required this. The code I want is in a NPM package, whereas I don’t want to build and configure a Node.js server in order to serve just a simple page (I’m a bit lazy). While it’s not recommended, you can have your PHP code execute javascript on the server itself.

You must have node installed, and you must provide the proper relative path from the directory of the PHP file being requested. For example, if the index.php file is requesting a ‘main’ javascript file in the same directory, make you can call the javascript file simply as `node main.js`.

// index.php
// Execute JavaScript server-side
exec("node main.js 2>&1", $output);
echo $output[0]; // Echo the main output


// main.js
// Make sure this file is in the same directory, or adjust the exec path as necessary
console.log("Hello, from the Server!")

On its own, right now, this script is minimally dangerous. The true danger comes when you start passing variables to the JavaScript code – which you should just avoid all together. If you absolutely need to pass variables, consider having the PHP script write them to a temp file, and then have the JavaScript file pick them up. It’s not the easiest thing to do, but the last thing you want to do is have someone pass their name as `sudo rm -rf /` to your site.

Happy Coding!