In your apache configuration:
AddHandler php-script .js
This tells apache to handle *.js files as PHP scripts.
In your apache configuration:
AddHandler php-script .js
This tells apache to handle *.js files as PHP scripts.
In an .htaccess file:
AddHandler application/x-httpd-php .js
Make sure you set the header content type to application/javascript
before you output anything:
header('Content-Type: application/javascript');
EDIT
After reading the edited question I think I understand.
If you take a JavaScript file which has PHP within it:
<?php
$var = 'this is a example';
?>
var foo = 'javascript bar';
You can include this file into a PHP script and access the variable $var
, however whatever is outside of the <?php ?>
will be outputted to the screen. You could then include this same exact file as a javascript src file and access the variable foo
from within JavaScript. I have no idea why you would actually want to do this though.
If you tell us what you are trying to accomplish we might be able to offer alternate solutions.
Any file of any kind can be included
/required
, including images. To PHP, it's all relatively just text. All that matters to be able to execute PHP code is what's inside the <?php ?>
block. When included
, all text found within the open/close tags will be executed as PHP, while anything outside will render as normal text.
To test this out, simply open a .JPG file in notepad, add <?php phpinfo(); ?>
to the very end, save, then include
this file in a PHP script like you would a PHP script file. You'll see a jumble of text, but after that, the full phpinfo dump. You can even use Content-type: image/jpg
header to output the page as a true image, but the full phpinfo dump will stil be hidden within.
Edit: be sure to properly label your mime type on your outputted page, as evolve said above, as a courtesy to the site visitor.
I have a .js file with code compatible for both php and js languages
hmmm, yes it is possible but I suspect your code is NOT compatible. Include/require works regardless of what the file extension is.
s there any way that i can require this file in my php script and the file may execute normally as a require/include would work on a .php file
You seem to think it's not compatible either.
Your post is so confused its hard to tell what you're really trying to achieve here - but the following code is valid in both languages and would execute where the relevant functions are defined:
// <?php
hello_world("I am bilingual!");