PHP can be written in js, but the file must be in php file format, or have a relevant replacement execution mechanism. The code is like "<script>var js_name=<?php echo $name;?> ;alert(js_name);</script>".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
Can I write php in js?
able. If javascript is included through a js file, then the php code can also be written directly in the js file, but the extension of the included js file must be changed to php.
That is to say, you can write php in js, but the file must be in php file format, or there is a relevant replacement execution mechanism.
Example: test.php
<?php $name="百度知道 "; ?> <html> <head> <title>JS PHP 测试</title> </head> <body> <script> //js调用php var js_name=<?php echo $name;?>; alert(js_name); </script> </body> </html>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Can I write php in js?. For more information, please follow other related articles on the PHP Chinese website!