Javascript Course

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 create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong></strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert(""); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
How to use php variable in external js file

Last accessed pages

  1. Writing PHP scripts (10393)
  2. PHP getElementById and getElementsByTagName (39781)
  3. Zodiac Signs JavaScript code (6961)
  4. Get Mime Type of file or string content in PHP (2723)
  5. setTimeout and this with bind() method in JavaScript class (2043)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (714)
  2. Read Excel file data in PHP - PhpExcelReader (350)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (348)
  4. querySelector and querySelectorAll (330)
  5. JavaScript Course - Free lessons (299)