Home >Backend Development >PHP Problem >Can php run js code?
php can run js code. In PHP, HTML code and JavaScript code are treated as strings and then parsed in the browser.
How to run JavaScript code in PHP:
Example 1: Writing JavaScript code in PHP code
<?php header("content-type:text/html;charset=utf-8"); echo '<script type="text/javascript">'; echo 'alert("php中文网");'; echo '</script>'; ?>
Output:
Example 2: Writing JavaScript code outside of PHP code (in the same PHP file)
<?php header("content-type:text/html;charset=utf-8"); //php代码 ?> <script type="text/javascript"> alert("欢迎来到php中文网"); </script>
Output:
Example 3: JavaScript function - DOM manipulation (in the same PHP file)
<?php echo "<div id='demo'></div>"; ?> <script type="text/JavaScript"> // 调用函数 var x = myFunction(11, 10); document.getElementById("demo").innerHTML = x; // 函数返回a和b的乘积 function myFunction(a, b) { return a * b; } </script>
Output:
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of Can php run js code?. For more information, please follow other related articles on the PHP Chinese website!