Home > database >  PHP: Does echo wait until a function ends before echoing?
PHP: Does echo wait until a function ends before echoing?

Time:02-10

I'm learning PHP and am going through some source code I found. In it there's an ajax request as follows:

$.post("includes/handlers/ajax_search.php", {query:value, userLoggedIn: user}, function(data) {
    //uses 'data' variable
});

What I'm unsure about is, the below code is in the called function and would appear to call echo multiple times. Does the called ajax function wait for all echoes to concatenate before actually echoing or does it act like a sort of a stream?

Some clarification would be really appreciated? Thanks.

while($row = mysqli_fetch_array($usersReturnedQuery)) {
        $user = new User($con, $userLoggedIn);

        if($row['username'] != $userLoggedIn)
            $mutual_friends = $user->getMutualFriends($row['username']) . " friends in common";
        else 
            $mutual_friends == "";

        echo "<div class='resultDisplay'>
                <a href='" . $row['username'] . "' style='color: #1485BD'>
                    <div class='liveSearchProfilePic'>
                        <img src='" . $row['profile_pic'] ."'>
                    </div>

                    <div class='liveSearchText'>
                        " . $row['first_name'] . " " . $row['last_name'] . "
                        <p>" . $row['username'] ."</p>
                        <p id='grey'>" . $mutual_friends ."</p>
                    </div>
                </a>
            </div>";

    }

CodePudding user response:

Echo outputs lines instantly. However, PHP has a system write buffer. There is flush() function to instantly output buffer to the output.

However, be mindful, that flush() only flushes PHP buffer, there might be another buffers between PHP and browser: web server's buffer, for example.

  • Related