Under what conditions can I put PHP into Javascript code? Is it always ok? Is it just a bad idea? I am surprised I have not seen it more.
For example, I came across this..
$('#thumbnail').imgAreaSelect({aspectRatio: '<?php echo $thumb_height/$thumb_width;?>', onSelectChange:preview});
..which was in an onpage script element. I’m not so experienced, but I was just surprised that I had never seen mixed js/php before. But can you put PHP in .js files as well? If I set the file extension to .php, will the file be parsed?
You can put PHP code into
*.jsfiles but you need to tell the webserver to also execute the PHP interpreter on*.jsfiles. Something like this should be in yourhttpd.confA better solution is to just rename your
*.jsfiles to*.js.phpor just*.php. The browsers won’t mind and the PHP interpreter will also get executed on those.