Is it possible to add a php code in a .js file? [duplicate]

0

Well my question is if you can add a code

If not, how can I do it to be possible.

Thank you

    
asked by anonymous 27.02.2016 / 15:05

1 answer

2

Rename the .js file to .php, after that at the beginning of the file, add the following.

<?php
    header("Content-type: text/javascript");
?>

Now you can include PHP codes both within these tags that we create, as in any other part of the file, remembering to follow the basic PHP rules.

This file will go through the PHP interpreter before it is sent to the client (as with an .php file with HTML, for example), and when it arrives on the client, it will be a common .js file.

    
27.02.2016 / 17:03