How to output js statements in php?
Example
<?php $classState=""; if($state==0){ $classState="get out of class has been discontinued"; } else{ $classState="In class"; } echo "<script type='text/javascript'> function getState(){ var cs = ".$state."; return cs; } </script>"; ?>
In this way, you can directly refer to the js method output in php in other places on the page.
Garbage problem when using echo js code on php page
When using php as the control script for the html front-end page, you often need to display something in the foreground. For example, use echo output. There is a requirement on the project recently, I have used itecho "<script></script>"
Such code is completely normal to debug locally, but once it is deployed to the server, js will be output to the foreground and garbled code will be displayed. I looked it online, and some people said it can be set<script type="charset:utf-8;"></script>
, but after setting this up, I found that the problem is still there. Analysis: The reason why garbled code exists is that the page encoding is inconsistent with the browser display encoding, or in other words, the front-end html page and the background control page encoding are inconsistent. So, first set the php encoding and use the header function.header("Content-type:text/html;charset:utf-8")
. Then refresh the browser and display it normally. The explanation should be that the php encoding is inconsistent with the browser's parsing page encoding.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below