Have you ever tried to write code like this?
Most of the time, you will find that the external.js file will have an error because it cannot make sense of the PHP snippet. The following is how you can make that code work.
Update the server rules
Within any folder of your server structure, you can place a “.htaccess’ file. This file tells the server a number of things about permissions, caching, permissions, etc. Specifically, this is where rules on which file extensions are run through the server language processes before those files are served to the client.
In this case, we want to run ‘.js’ files through the PHP parser before the ‘.js’ file is served to the client. To do that, we add this to the ‘.htaccess’ file:
Options +ExecCGI AddHandler x-httpd-php .js
If you are a GoDaddy client, they have more detail here:
Change the Extension
Options +ExecCGI AddHandler x-httpd-php .php.js
Trick the Browser
That’s all there is too it.