0

lib.php:

<?php

function get_time() {
    loop = true;
    while (loop) {
        echo date("F j, Y, g:i a");
    }
}

?>

index.php contains this:

<?php
    include("lib.php");
?>

and this somewhere further down:

<?php
    get_time();
?>

However, I don't see anything.

So I see two potential problems here. Either I need to declare the include() at the same time as I declare the function. Or PHP is unable to display continuous data (like in the case of this while loop) on a webpage without refreshing.

If it's the former, is there a way to declare the include() globally for my whole page? If it's the latter, how can I get it to show the contents of a continuous loop on my page? Will I have to use another language? Thanks

7
  • 1
    What are you trying to accomplish? Are you wanting the time to update automatically as the user is visiting the page? If so, you are seriously misunderstanding the relationship of client/server and should take some tutorials on JavaScript and AJAX. Commented Jul 27, 2011 at 17:54
  • 1
    PHP has no problem running infinite loops, though you'll most likely have it killed when max_execution_time is exceeded. However, check your error logs. You're using include(), which will not abort execution if lib.php cannot be found. Turn on error_reporting and display_errors to see what shows up. Commented Jul 27, 2011 at 17:54
  • 3
    you have an indefinite loop for your time Commented Jul 27, 2011 at 17:56
  • 1
    I'm trying to get it update the server's time in realtime. Commented Jul 27, 2011 at 17:56
  • 1
    Make sure you put $ before PHP variables. $loop = true not loop = true; Commented Jul 27, 2011 at 17:58

3 Answers 3

4

This piece of code will loop infinitely:

function get_time() {
    loop = true;
    while (loop) {
        echo date("F j, Y, g:i a");
    }
}

get_time();

So the fact that you do not see anything is probably because of this.

Also, you are correct - to see continuously updated data like this, you either need to be refreshing the page, or update the content through AJAX calls.

Sign up to request clarification or add additional context in comments.

3 Comments

But I should be seeing the date shouldn't I? I'm echo'ing it.
Alright so is there a way for me to display updated information? I should learn AJAX?
@MaxMackie - this isnt how PHP works. You cannot display continuously updated content in this manner. I'd look into AJAX or timed page refreshes for this sort of behaviour.
2

Most of the time, PHP is not able to send data to your browser multiple times because (continously):

  • Except Firefox, no browser is able to display incomplete data.
  • Your web server probably buffers the data that's outputted by your FastCGI/CGI/PHP-module.
  • You don't use ob_implicit_flush(1) at the start of your get_time()-function, nor do you have flush(); after each echo (so you could try adding this line at the end of your while-body).

Comments

2

If that's the code you're using, make sure your variables have dollar signs.

$loop = true;
while ($loop) {
...
}

1 Comment

That won't matter. PHP automatically recognizes variable-names without a dollar-sign as constant (instead of plain variables), it'll still be able to store and retrieve the data of the variable. The only reason not to use constants is because constants are global (so they may interfere with the rest of your codebase) and because their performance is significantly worse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.