为什么PHP代码在HTML之前执行? [英] Why is the PHP code executing before the HTML?

查看:203
本文介绍了为什么PHP代码在HTML之前执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习了PHP几个星期,现在我正在编写一个结合了MySQL数据库,OOP PHP和PHP会话的脚本。

,如果你已经登录,那么网站上会显示你的名字(如果你设置了会话变量),如果你没有登录,登录框就会显示。

 <?php 
if(!isset($ _ SESSION ['username'])){
echo< h3> Login< ; / h3>
include(login.php);
} else {
echo< h3> Welcome< / h3> 。$ user-> get_name();
echo< br>< a href ='logout.php'>注销< / a>;
} $ b $假设有一个会话,上面的代码应该输出:
>



欢迎Alex!



然而,如果我运行它,结果是:

  Alex 
欢迎

为了快速修复它,我发现如果我这样做:

  echo< h3>欢迎< / h3> ; 
echo $ user-> get_name();
echo< br>< a href ='logout.php'>注销< / a>;

它修复了它。也就是说,如果我将声明分解为两个回声并且文本输出正确。当然,我知道这可能不是一个好方法来解决它,因为我需要添加以下代码:

  echo < img src ='。$ user-> get_photo()。border ='5''>; 

再一次,图片的文字先载入。

输出是文本中的图像链接,并且该chrome图标用于模拟未加载图像。



链接到图像输出



有人可以帮我吗?谢谢!如果需要代码的任何部分,请告诉我,我只写了我认为相关的内容,但我可能是错的。



编辑:



get_name()函数的代码:

  function get_name(){
echo get_user_data($ this-> user_id,name);


解决方案

echo 执行IMMEDIATE输出。如果您尝试回显本身执行回显的函数,则函数的回显会执行FIRST。例如

  function foo(){
echo'foo';
}

echo'bar'。 FOO(); //输出foobar

并执行相当于

  echo'foo'; 
echo'bar';

为什么?因为 echo 首先必须构造输出的字符串。因此,在 bar 可以被回显之前,父回声必须调用 foo()。该函数不返回任何东西,它只是执行自己的回声。多个 echo 调用不会相互协调,所以foo的echo会输出它。那么foo不会向父回声返回任何内容,因此您正在执行 echo'bar'。 null ,并输出 bar



如果你有这个:

 函数bar(){
return'bar';
}

echo'foo'。酒吧();

它将按预期工作。执行顺序为:

  $ temp = bar(); // $ temp得到string'bar'
echo'foo'。 $温度;
回声'foo'。 '酒吧';
echo'foobar';
- >输出foobar


I've been learning PHP for some weeks and right now I'm writing a script that combines a MySQL database, OOP PHP and PHP sessions.

Right now, theres a bit on the website that shows your name if you're logged in (that is if the session variable is set), and a log-in box if you're not logged in.

 <?php
                if(!isset($_SESSION['username'])) {
                    echo "<h3>Login</h3>
                     include("login.php");
                } else {
                    echo "<h3>Welcome</h3> ". $user->get_name();
                    echo"<br><a href='logout.php'>Log-out</a>";
                }
            ?>

Assuming there is a session, the code above should be outputting: Welcome Alex!

However, if I run it the result is:

Alex
Welcome

In order to quickly fix it, I found out that if I do:

 echo "<h3>Welcome</h3> ;
    echo $user->get_name();
    echo"<br><a href='logout.php'>Log-out</a>";

It fixes it. That is, if I break the statement into two echos and the texts outputs correctly. Of course I know that's probably not a good way to fix it, given that I need to add the following code:

echo "<img src='".$user->get_photo()." border='5''>";

and once again, the text of the image loads first.

The output is the image link in text, and that chrome icon that simbolizes that an image was not loaded.

link to image output

Can someone help me out? Thank you! If any opther bits of the code are needed please tell me, I've only written what I think it's relevant but I might be wrong.

EDIT:

Code of "get_name()" function:

function get_name() {
            echo get_user_data($this->user_id,"name");
        }

解决方案

echo does IMMEDIATE output. If you try to echo a function which itself does an echo, the function's echo executes FIRST. e.g.

function foo() {
   echo 'foo';
}

echo 'bar' . foo();   // output foobar

and executes as the equivalent of

echo 'foo';
echo 'bar';

Why? Because echo first has to construct the string being output. So before bar can be echoed, the parent echo has to call foo(). That function doesn't return anything, it simply performs its own echo. multiple echo calls do not coordinate with each other, so foo's echo does its output. then foo returns nothing to the parent echo, so you're doing echo 'bar' . null, and output bar.

If you had this:

function bar() {
   return 'bar';
}

echo 'foo' . bar();

it would work as expected. The execution sequence would be:

$temp = bar(); // $temp gets string'bar'
echo 'foo' . $temp;
echo 'foo' . 'bar';
echo 'foobar';
-> output foobar

这篇关于为什么PHP代码在HTML之前执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆