Usually you can't add php code in external javascript (js) file. But, you can add and use php code in the page in which the external javascript file is included.
So, you can apply the following technique to use value of php variable in js file.

1. In the page in which the external javascript file is included, output the value of the php variable to a javascript variable:
//php code..
$some_var ='value';
<!doctype html>
<html lang='en'>
<meta charset='utf-8' />
<title>Page Title</title>
var js_var ='<?php echo $some_var; ?>';
Page content..

<script src='external_file.js'></script>

Now, in the script from external_file.js we can use the js_var (it contains the value of the php $some_var).
2. For example, in external_file.js:

alert(js_var); // value

The javascript variable must be defined before the external js file is included.

Daily Test with Code Example

Which tag adds an image in web page?
<div> <img> <span>
<img src="" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
How to use php variable in external js file

Last accessed pages

  1. Register and show online users and visitors (27583)
  2. Prevent Hotlinking / Block External Access to Video and Audio files (2974)
  3. Numbers and Math in ActionScript 3 (3281)
  4. Using slideDown and SlideUp (1576)
  5. Send Email with Nodemailer (1077)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (2460)
  2. Contact page - CoursesWeb (2376)
  3. Tabs effect with CSS (2366)
  4. Insert, Select and Update NULL value in MySQL (1356)
  5. PHP getElementById and getElementsByTagName (933)