Home > Backend Development > PHP Problem > Can I write php in js?

Can I write php in js?

藏色散人
Release: 2023-03-14 20:16:02
Original
3476 people have browsed it

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>".

Can I write php in js?

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template