SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    North Carolina, USA
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recursive Functions & References

    Here's a script segment:

    Code:
    ...
    
    $my_array = array();
    
    ...
    
    some_recursive_function(&$my_array);
    
    ...
    
    function some_recursive_function (&$reference_array) {
       ...
    
       if ($continue_with_recursion) {
          some_recursive_function($reference_array);
       }
    }
    I have a function similar to this (the overall structure of it at least). For some reason the original array ($my_array) is not being modified when function 'some_recursive_function' is executed. And I know for a fact that the recursive function is supposed to execute at least a few times (after checking (echo()ing the conditions by printing them out). Is my code up there correct?

    Thanks.

  2. #2
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't the call to itself inside the function have the reference ampersand also?
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    North Carolina, USA
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried doing that and it still didn't work. I would post the function that I have, but it's really long...

  4. #4
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My guess is there's a bug somewhere you've missed.

    Here's a simple example that works;

    PHP Code:
    <?php
    $my_array 
    = array();

    some_recursive_function($my_array);

    function 
    some_recursive_function (&$my_array) {
        
    $my_array[]='another value';
        
    $size=count($my_array);
        if (
    $size<10) {
            
    some_recursive_function($my_array);
        }
    }

    echo ( 
    '<pre>' );
    print_r($my_array);
    echo ( 
    '</pre>' );
    ?>
    Note that you shouldn't place the reference operator in a function call as you do here;

    PHP Code:
    some_recursive_function(&$my_array); 
    This isn't your problem (you can get away with it fine) but it's bad practice (see php.ini - allow_call_time_pass_reference setting [or something like that]).

    Note that in my example, if I remove the single reference operator it breaks;

    PHP Code:
    <?php
    $my_array 
    = array();

    some_recursive_function($my_array);

    function 
    some_recursive_function ($my_array) {
        
    $my_array[]='another value';
        
    $size=count($my_array);
        if (
    $size<10) {
            
    some_recursive_function($my_array);
        }
    }

    echo ( 
    '<pre>' );
    print_r($my_array);
    echo ( 
    '</pre>' );
    ?>
    This displays an empty array.

    Suggest break the code down into smaller pieces (i.e. extra functions) and work through it step by step - probably something you've missed if it's a big function


Bookmarks

Posting Permissions

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