Skip to content Skip to sidebar Skip to footer

Why Is The Php Code Executing Before The Html?

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

Solution 1:

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

functionfoo() {
   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:

functionbar() {
   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

Solution 2:

change the structure of function get_name() to

functionget_name() {
            return get_user_data($this->user_id,"name");
        }

and it would work fine

Post a Comment for "Why Is The Php Code Executing Before The Html?"