#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    303
    Rep Power
    17

    Question about counting in loops and functions


    Hi

    I have a questions about counting in and outside of functions, here is my example:

    PHP Code:
    $x 0;

    foreach (
    $xml->data as $k => $v){
       
    //... do something
       
    $x++;
    }
    if(
    $xml->more != ''){
         
    $x execute_more($x);
    }else{
         print 
    'Executed' $x' times.'


    function 
    execute_more($x){
        foreach (
    $xml->data as $k => $v){
             
    // do something
            
    $x++;
        }
        if (
    $xml->more != ''){
           
    execute_more($x);
        }else{
           return 
    $x;
       }

    if the script only runs one time the value in $x is correct. If it runs more times the value in $x is empty. Anybody know where I am wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,971
    Rep Power
    375
    the code is just wrong, you are doing same thing outsidde the function and then same thing inside!!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    303
    Rep Power
    17
    Yes I know,

    because I figured out I would need the recursive part after I had wriiten the first part.

    So then I could remove the first part and just use the recursive function. Without running it the first time outside of the function.

    Technically that would be a bit better. The end result is still the same. Just I need to track the count $x throughout the whole thing.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    You cannot simply access the $xml variable from the function. PHP isn't JavaScript. It usually receives all input through its parameters (with some exceptions).

    The approach generally doesn't make a lot of sense. Yes, recursion is cool. But often times, a plain loop is much easier to understand.

    Either way, before you start to write code and worry about technical details, get clear about what you're trying to do.

    According to your current code, you want to repeatedly loop through $xml->data until $xml->more becomes a truthy value. And all iterations are counted.

    So the pseudo code would be:

    Code:
    iterations := 0
    
    do and repeat while $xml->more:
        for each item in $xml_data:
            // do something
            iterations := iterations + 1
        for.	
    end do.
    This kind of do x and repeat it while y is implemented with a do-while loop:

    PHP Code:
    <?php

    $iterations 
    0;

    do
    {
        foreach (
    $xml->data as $xml_key => $xml_value)
        {
            
    // do something
            
    $iterations++;
        }
    }
    while (
    $xml->more);

    print 
    'Executed ' $iterations ' times.';
    Note that this is just a direct translations of your code. I don't know the context, so the approach itself may already be flawed.
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    Your execute_more function exits without returning anything when it recurses. Assuming that $xml is global, the function needs to look like this:
    PHP Code:
    function execute_more($x){
        foreach (
    $xml->data as $k => $v){
            
    // do something
            
    $x++;
        }
        if (
    $xml->more != ''){
            return 
    execute_more($x);
        }else{
            return 
    $x;
        }

    Or, if you prefer only one return, this:
    PHP Code:
    function execute_more($x){
        foreach (
    $xml->data as $k => $v){
            
    // do something
            
    $x++;
        }
        if (
    $xml->more != ''){
            
    $x execute_more($x);
        }
        return 
    $x;

    Comments on this post

    • Jacques1 disagrees : Would you please stop digging out tons of old threads that have long been solved?
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Um, no. Please check how scoping in PHP works and read the previous replies.

    As I already said above, PHP is not JavaScript. Functions do not have access to outer scopes unless you explicitly import the variables. For global variables, there are two ways.

    Either you use the global keyword:

    PHP Code:
    <?php

    $foo 
    'abc';

    function 
    f()
    {
        global 
    $foo;
        
        echo 
    $foo;
    }

    f();
    Or you use the $GLOBALS superglobal:

    PHP Code:
    <?php

    $foo 
    'abc';

    function 
    f()
    {
        
    $foo $GLOBALS['foo'];
        
        echo 
    $foo;
    }

    f();
    But as I already said, recursion isn't very useful for this. I know it's the favorite toy of many programmers, but a simple loop does the same thing in less lines and much more readable.

    Comments on this post

    • Clone53421 disagrees : That's still not why $x is ending up empty. It's ending up empty because he forgot to return it.
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo