Skip to content
Related Articles

Related Articles

How to run JavaScript from PHP?
  • Difficulty Level : Medium
  • Last Updated : 08 Dec, 2018
GeeksforGeeks - Summer Carnival Banner

JavaScript is the client side scripting language and PHP is the server side scripting language. JavaScript is used as client side to check and verify client details and PHP is server side used to interact with database. In PHP, HTML is used as a string in the code. In order to render it to the browser, we produce JavaScript code as a string in the PHP code.

Example 1: Write JavaScript code within PHP code

echo '<script type="text/JavaScript"


Example 2: Write JavaScript code outside of PHP code (in same PHP file)

    // some php stuff
<script type="text/javascript">


Example 3: JavaScript Function – DOM Manipulation (in same PHP file)

    echo "<div id='demo'></div>";
<script type="text/JavaScript">
// Function is called, return 
// value will end up in x
var x = myFunction(11, 10);   
document.getElementById("demo").innerHTML = x;
// Function returns the product of a and b
function myFunction(a, b) {
    return a * b;             



Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :