Results 1 to 9 of 9
  1. #1
    Join Date
    May 2004
    Posts
    500

    [php] infinite loops

    If I made a script, say:
    PHP Code:
    <?PHP

    $var 
    'spin me right round baby right round';

    while(
    $var == 'spin me right round baby right round'){
    echo 
    $var;
    $var $var;
    }

    ?>
    It would just keep looping round and round in circles. Would that mess up the server? I would try it to find out, but my host probably wouldn't be happy if I crashed their server, and I haven't got around to installing apache and stuff on my PC since I reformatted yesterday.

  2. #2
    Join Date
    Jun 2004
    Posts
    109
    This should answer your question:
    http://www.php.net/manual/en/ref.inf...execution-time


    Your script will terminate after it has run for the max_execution_time.
    You will probably see some form of error notice on the web page, rather than the server crashing. No self-respecting scripting language would allow the server to crash based on user input

  3. #3
    Your server can handle this. The only result is the thread responsible for this request will time out. Other threads won't be affected.

  4. #4
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    A simple infinite loop is :

    PHP Code:
    <?php

        
    while(1) { echo 'Whee!!<br />'; }
    ?>
    And yes, your server can handle this because your php script will time out after the set time limit is reached.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  5. #5
    It all depends on what your 'infinite' loop does. If all it does is echo static strings, its not a problem.

    If however it processes a ton of files, using preg_replace, making database connections, or otherwise generating a lot of CPU or I/O intensive activities, yes it will be a problem

    To make your loop infinite you need to make the timeout '0'.
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

  6. #6
    Join Date
    Dec 2003
    Location
    Vancouver BC, eh?
    Posts
    570
    Isn't there a maximum amount of memory php can use?

  7. #7
    Join Date
    Jun 2004
    Posts
    109
    memory_limit is a directive in php.ini that does this.

    There are also other directives which limit memory allocated for _POST data, uploaded files, etc...but they are all on that same page I linked you to

  8. #8
    Join Date
    May 2004
    Posts
    500
    Ok, thanks for all the replies =) I figured it would be a bit stupid for PHP to let a script crash the server as easily as that.

  9. #9
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •