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
<?php echo '<script type="text/JavaScript"> prompt("GeeksForGeeks"); </script>' ; ?> |
Output:

Example 2: Write JavaScript code outside of PHP code (in same PHP file)
<?php // some php stuff ?> <script type="text/javascript"> alert('GeeksforGeeks!'); </script> |
Output:

Example 3: JavaScript Function – DOM Manipulation (in same PHP file)
<?php 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; } </script> |
Output:
110
Recommended Posts:
- How do you run JavaScript script through the Terminal?
- How to run a function when the page is loaded in JavaScript ?
- Create a web server and run PHP script on it using Raspberry Pi
- How to run two PHP versions for two projects on same apache server ?
- Run Python script from Node.js using child process spawn() method
- How to run Java RMI Application
- Why is Java 'write once and run anywhere'?
- How to Run a Python Script
- How to run Cron Jobs in Node.js ?
- How to run two animations simultaneously in jQuery ?
- How to Run Synchronous Queries using sync-sql Module in Node.js ?
- Docker compose tool to run multi container applications
- How to Make, Save, and Run a Simple VBScript Program?
- Run Python Script using PythonShell from Node.js
- How to Run Multiple Versions of Node.js ?
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


