Home > Backend Development > PHP Problem > How to correctly output JS statements in PHP

How to correctly output JS statements in PHP

autoload
Release: 2023-03-09 07:10:02
Original
3648 people have browsed it

How to correctly output JS statements in PHP

A computer program is a series of "instructions" that are "executed" by the computer. In programming languages, these programming instructions are called statements. A JavaScript program is a series of programming statements. JavaScript statements are composed of: values, operators, expressions, keywords, and comments

1. Method:

echo "<script></script>";
Copy after login

2. Example:

<?php
$classState="";
if($state==0){
 $classState="已下课";
 }else{
 $classState="正在上课";
 }
echo "<script type=&#39;text/javascript&#39;>
function getState(){      
    var cs = ".$state.";
    return cs;
} 
</script>";
?>
Copy after login

3. JS garbled problem occurs:

header("Content-type:text/html;charset:utf-8")
<script type="charset:utf-8;"></script>
Copy after login

Keep PHP encoding is consistent with the browser parsed page encoding .

Recommended: "php video tutorial" "php tutorial"

The above is the detailed content of How to correctly output JS statements in PHP. 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