Home >Backend Development >PHP Tutorial >Why does php return a piece of js code?
I don’t understand why php returns a js string? For example: echo '<script>window.location.href="/index.php/Admin/Login"</script>';
I don’t understand why php returns a js string? For example: echo '<script>window.location.href="/index.php/Admin/Login"</script>';
JS cannot be executed in PHP. JS can only be executed after PHP is parsed into HTML. If you want to use php to output js, you must output the js code as a php string.
This should be for jumping to the page. There is nothing surprising. It depends on the specific needs and application scenarios. For example, the following code
(In php) js is not run on the server, it should be on the client.
php just returns the string "<script></script>" to the front desk and does not execute the statements inside. If you display the string on the browser, the browser will automatically compile and execute the script .
You can consider using file_get_contents($url);
Usually judged in the web page request, if there is no login jump
<code class="php">if(!$login){ // 和输出字符串没区别,只不过会被js执行 echo '<script>window.location.href="/index.php/Admin/Login"</script>'; } </code>
The questioner probably wants to ask why it didn’t jump, right?
I’m not sure what the question means. I suggest you read The Art of Questioning
...
php can return any code, there is nothing to discuss about this
An intern I taught before asked me why PHP can’t throw exceptions to the browser. Now a female colleague once tried to take a variable in JS in PHP to make a judgment. There is also the subject’s question. Basically None of them understand how a request is completed, and how the program is executed.
Let me try to explain: When using PHP to develop on the server side, PHP, HTML, JS, and CSS codes are often written together, especially when PHP code is mixed with the latter three (less CSS). But in fact, during execution, these parts are executed independently.
PHP can only be executed on the server side. After the request is sent, the server executes the PHP script. For the PHP interpreter, it can only see and execute the part of the code in the middle of . After executing this part After the code, the data required for the request will be generated. Sometimes the data is a json string, and sometimes this thing is a page containing HTML, JS, and CSS codes.
The JS code generated by PHP as asked by the question is generated at this time. For the PHP interpreter, those JS codes are no different from ordinary strings.
After this, the server reads the data from the PHP buffer (such as a page containing HTML, JS and CSS code), and then packages the data and sends it to the browser. The browser has built-in interpreters for HTML, JS, and CSS, so the browser can execute HTML, JS, and CSS codes. Similarly, because the browser does not have a PHP interpreter, the browser cannot execute PHP code. The questioner can try to write a static page locally, write a piece of PHP code in it, and then open it with a browser to observe.
To put it simply, it is like there is such a wall. One side of the wall is the browser, which can execute HTML, JS and CSS; the other side of the wall is the server, which can execute PHP; the two interact through the HTTP protocol. For the browser, because it does not understand PHP code, even if it is given a piece of PHP code, it is just a bunch of ordinary strings to it. Similarly, for the server, the so-called HTML, JS and CSS code are no different from "Hello, world".
Both JS and PHP can generate any form of string, so they can generate code in any language.
Going further, if there is no program to interpret and execute, any code is just a bunch of strings.
Some personal opinions, welcome to correct me.
For the answer, see @清春月下’s answer under the answer.
In the asp.net webform era, there was an operation of "server-side registration script to client". After all, the front-end had not yet emerged at that time.
You should split this question into two questions:
I don’t understand why php returns a piece of html code?
I don’t understand why a js code is included in html?
Big mistake~
jsonp, a programming method that can cross domains.