A: 

In your apache configuration:

AddHandler php-script .js

This tells apache to handle *.js files as PHP scripts.

Andrew Sledge
But then he wouldn't be able to use it on the client side as a JS or any other JS for that matter.
Alin Purcaru
should still work as long as JS code isn't inside <?php ?> blocks, and the user isn't viewing the .JS file directly as the HREF.
Fosco
@Alin, Fosco is right, it has to be run through the PHP processor.
Andrew Sledge
@Andrew and Fosco, But the PHP code has to be inside <?php ?> blocks so it can be included ;).
Alin Purcaru
okay I get it, I think I shouldnt use this approach then coz I dont want to make such an .htaccess change for this thing, I'll keep my data as a json string then, will have the overhead of json_decode and json_encode while editing of the file but i guess there is no other solution
Umair
+1  A: 

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.

evolve
Whoever down voted, feel free to leave a comment explaining why. This was the original answer to the question before he edited.
evolve
A: 

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.

bob-the-destroyer
+1  A: 

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!");
symcbean
wow i am getting some hints here, what i did was not adding the <?php in that js file, a big mistake, but will this code block work in both? when included via php and when called as a js// <?phphello_world("I am bilingual!");
Umair
how can I manage with <?php, to work only when it is included via php
Umair
The problem with your example, @symcbean is that if `hello_world()` outputs anything, it will be on the same line as the comment. Could be fixed by `print()` or `echo` ing a line break: `echo "\n";` before any output is generated.
sholsinger
@shotsinger: If I was trying to create a javascript file from a php script then yes - but read my post again - I've written code which runs in both PHP javascript - not the same thing at all.
symcbean
I got my clue, tried it and it worked. Thanks symcbean :D
Umair