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

How to correctly output JS statements in PHP

autoload
autoloadOriginal
2021-03-26 16:47:543616browse

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

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>";
?>

3. JS garbled problem occurs:

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

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!

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