Loop Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Loop Problem

Post by thomas777neo »

I have a loop that checks a file for a value, if the value is 1, do something:

Code: Select all

if (file_exists($file_name))
{
	while (true)
	{
		$read = file($file_name);
		$read = $read[0];
		
		if ($read == 0)
		{
			sleep(5);
		}
			else
			{
                        echo "Worked!!!";
                     }
       }
}
Which works fine, as soon as I update the value of 0 in the file to 1, it shows the text.

The problem is that if I have launched the page, I cannot go anywhere else on my site, it just hangs. And if the file value was indeed 1, and I refresh the page. It executes the php before the html is shown, so the user would see the previous html and not the new html.

What can be done to get around this problem?
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

PHP will always process at the server every time the page is called. It is also procedural by nature, though we are working to get past that so its procedural nature will make it more difficult since you can not run a stateless enviroment.

To prevent the looping effect from being seen on the browser, a refresh with a session variable might be in order.

You may also consider using sockets.

Hope my thoughts help,

Chris
Post Reply