PHP memory limit exceeded problem...?

Discussion in 'Mac Programming' started by Jiddick ExRex, Jan 12, 2008.

  1. Jiddick ExRex macrumors 65816

    Jiddick ExRex

    Joined:
    May 14, 2006
    Location:
    Roskilde, DK
    #1
    I am having some trouble with a script I have been writing. It worked out fine the first 10 or so times I executed it but now I get this error: Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 16 bytes) [...] in line 25.

    Weird thing is that all the script is really doing is take a string, chop it up into a char array and setting the first char in the array to uppercase (to avoid inconsistencies in our database). I just don't know why I get the error now and not the first half an hour I used it.

    I have contacted our host but they tell me that I shouldn't exceed the memory limit (duh!) and that they cannot help. I just have this feeling that this is not my fault this time? How can the script exceed 16 mb when all it does is creating some more string and array variables?
    Code here:

    PHP:
    <?php

    function stringtoarray($string){
            
    $len strlen($string);

            
    $cur 0;

            
    $characterarray = array();

            while(
    $cur $len){
                
    $char $string{$cur};
                
    array_push($characterarray,$char);
                
    $cur $cur 1;
            }

            return 
    $character_array;

        }
    //_______________________________________________________________________________________//

        
    function correct_lowerc_error($string){
            
    $char_array stringtoarray($string);
            
    $char_array[0] = strtoupper($char_array[0]);
            
    $formatted_string implode(''$char_array);

            return 
    $formatted_string;
        }
        
    //_______________________________________________________________________________________//

        
    function words_to_upper($string){
            
    $string_array explode(" "$string);
            
    $i 0;
            while(
    $i <= sizeof($string_array)){
                
    $string_array[$i] = correct_lowerc_error($string_array[$i]);
                
    $i++;
            }

            
    $formatted_string implode(" "$string_array);
            return 
    $formatted_string;
        }




    $string 'vellensbyvejen strandberg';

    $formatted_string words_to_upper($string);

    echo 
    $formatted_string;

    ?>
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    I can't see it but I imagine you have an infinite loop somewhere. I did notice a different bug though:

    I think you want to return $formatted_string from function correct_lowerc_error not $string...
     
  3. Jiddick ExRex thread starter macrumors 65816

    Jiddick ExRex

    Joined:
    May 14, 2006
    Location:
    Roskilde, DK
    #3
    Thanks for that. It didn't solve my problem though :)

    I really can't find any infinite loops though. I think I have gone code-blind... :(
     
  4. Luveno macrumors member

    Luveno

    Joined:
    May 12, 2006
    Location:
    Nova Scotia, Canada
    #4
    I can see a few problems here, let's see what we can do to fix it.

    problem #1, in words_to_upper()
    PHP:
        function words_to_upper($string){
            
    $string_array explode(" "$string);
            
    $i 0;
            while(
    $i <= sizeof($string_array)){
                
    $string_array[$i] = correct_lowerc_error($string_array[$i]);
                
    $i++;
            }
            
    $formatted_string implode(" "$string_array);
            return 
    $formatted_string;
        }
    Error number one is in your while statement. You're starting $i equal to zero, and looping while it's less than or equal to the sizeof($stringarray). That sizeof() statement will return 2, which would be 3 iterations of $i. Since you're creating $string_array[$i] on each iteration, i'm guessing you're at a bit of a deadlock here, since you'll never meet your complete condition. First thing to do is change:

    PHP:
            while($i <= sizeof($string_array)){
    to :

    PHP:
            while($i sizeof($string_array)){

    That should allow your script to execute, but it will still give you nothing as a result. If you look at the function stringtoarray() you'll notice that you declare $characterarray as your main working array, and you push your char values onto $characterarray, but in the end, you return $character_array. All you need to do is remove the underscore in the variable name in your return statement, and your code should return the desired result.



    That being said, PHP offers a function to convert the first character of each word to uppercase, ucwords();

    A revised version of your code, making use of native (and faster) functions would be:

    PHP:
    <?php

        
    function words_to_upper($string){
            return 
    ucwords(strtolower($string));
        }




    $string 'vellensbyvejen strandberg';

    $formatted_string words_to_upper($string);

    echo 
    $formatted_string;


    ?>

    Hope that helps!
     

Share This Page