简体   繁体   English

PHP执行是如何工作的,它在网页中的位置如何影响输出?

[英]How exactly does PHP execution work and how does it's position in web page affect output?

I was messing around with php to better understand it's execution. 我在搞PHP,以更好地了解它的执行。 I found that when this same code is run with the php block placed at the end of the page, it produces a different output. 我发现,当将相同的代码与页面末尾的php块一起运行时,它会产生不同的输出。 Can somebody explain it to me. 有人可以向我解释。 This difference made me go crazy for days when I was working on my mini project. 当我从事迷你项目时,这种差异使我疯狂了好几天。

<?php
    if(isset($_POST["btn1"])){  
        echo "btn1";
        die();
    }   
?>
<html>
    <body>
        <button id="btn1" name="btn1">btn1</button>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("#btn1").click(function(){
                    $.ajax({
                        type:'post',
                        url:'',
                        data:{'btn1':true},
                        datatype:'text',
                        success:function(val){
                            alert(val);
                        }
                    });
                });
            });
        </script>
    </body>
</html>

PS. PS。 This code when run as is, produces the proper output ie it alerts "Hello". 此代码按原样运行时会产生正确的输出,即会警告“ Hello”。 But when the php code is written at the end, it alerts the html code of the whole page... 但是,当在末尾编写php代码时,它会警告整个页面的html代码...

PHP is processed inline unless it is wrapped in a function call, a class declaration, or a control statement since it is intended to be included within html. 除非将PHP封装在函数调用,类声明或控制语句中,否则它会进行内联处理,因为它打算包含在html中。 PHP = PHP Hypertext Preprocessor. PHP = PHP超文本预处理器。 http://php.net/manual/en/intro-whatis.php I repeat, PHP is processed inline. http://php.net/manual/en/intro-whatis.php我重复一遍,PHP是内联处理的。 The server does not load a file and run only the PHP or the HTML, it starts at the beginning of the file and processes the entire file unless there is a function call, class declaration, or some other control statement. 服务器不加载文件,而仅运行PHP或HTML,它从文件的开头开始并处理整个文件,除非存在函数调用,类声明或其他控制语句。

When your initial page loads as written, the 'btn1' parameter hasn't been posted so the if statement is skipped and the server continues executing the page to return the html in the rest of the doc. 当您的初始页面按写入方式加载时,尚未发布'btn1'参数,因此将跳过if语句,服务器将继续执行该页面以在文档的其余部分返回html。

When your ajax call reaches the page, it starts at the top and this time the if statement is true because btn1 is set. 当您的Ajax调用到达页面时,它从顶部开始,这一次,因为设置了btn1,所以if语句为true。 The die() call tells the server to just quit going through the rest of the page so you get "btn1" from the echo statement. die()调用告诉服务器退出页面的其余部分,以便从echo语句中获取“ btn1”。

If you remove the die statement, you'll get the "btn1" text and then the rest of the page from the ajax call. 如果删除die语句,则将获得“ btn1”文本,然后从ajax调用中获得页面的其余部分。 If you put the php block at the end of the page, you'll get the whole page and then the "btn1" text. 如果将php块放在页面的末尾,则会得到整个页面,然后是“ btn1”文本。 If you shove that php clock into the middle of the html, the ajax call will return the page with "btn1" in the middle where the code is. 如果您将php时钟拖入html的中间,则ajax调用将返回代码中间的“ btn1”页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM