PHP and JavaScript Relation
PHP and JavaScript Relation
What is JavaScript:
JavaScript was just a client-side scripting language up until 2009, which meant it could only be used in
a browser. Node.js, a server-side runtime environment that enables JavaScript to run on a web server,
has made it feasible to execute server-side JavaScript code to construct dynamic webpages, similar to
how PHP is used right now. (upwork.com, 2022)
Code:
<?php
echo '<script type="text/JavaScript">
prompt("GeeksForGeeks");
</script>'
;
?>
Output:
Example 2: Outside of the PHP code, write JavaScript code (in same PHP file)
Code:
<?php
// some php stuff
?>
<script type="text/javascript">
alert('GeeksforGeeks!');
</script>
Output:
Example 3: Manipulation of the DOM in JavaScript (in same PHP file)
Code:
<?php
echo "<div id='demo'></div>";
?>
<script type="text/JavaScript">
REFERENCES:
GeeksforGeeks. (2021, August 1). How to run JavaScript from PHP? https://www.geeksforgeeks.org/how-to-
run-javascript-from-php/
Upwork. (2022, February 1). PHP vs. JavaScript: Comparing Strengths and Weaknesses
https://www.upwork.com/resources/php-vs-javascript