search

Can php run js code?

Oct 14, 2019 pm 03:44 PM
javascriptjsphp

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 &#39;<script type="text/javascript">&#39;;
echo &#39;alert("php中文网");&#39;;
echo &#39;</script>&#39;;
?>

Output:

Can php run js code?

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:

Can php run js code?

Example 3: JavaScript function - DOM manipulation (in the same PHP file)

<?php 
    echo "<div id=&#39;demo&#39;></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:

Can php run js code?

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment