Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Simple Loop

  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Simple Loop

    I've got a bunch of if statements that look like this:
    PHP Code:
    if ($Variable1 !== "") {$VariableX $VariableX 1;}
    if (
    $Variable2 !== "") {$VariableX $VariableX 1;} 
    Basically, I need to perform this for Variables 1 - 50. The numbered variable always increases by 1 (Variable1, Variable2, Variable3, and so on), and VariableX stays the same throughout all of the if statements.

    Is there a loop of some sort I could use that would do this all automatically instead of manually for each individual variable?

    Thanks in advance.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,066
    Thanks
    2
    Thanked 319 Times in 311 Posts
    You need to use an array.

    When you have a set of same type data, using an array will result in the simplest and quickest code. Using consecutively named variables results in more complicated and the slowest operating code.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    instead of a whopping 50 vars, try putting them all in a array. Then you can use:

    Code:
    for($i=0; $i<50; $i++)
    {
    if ($variable[$i]!="")
    {
    $variableX+=1
    }
    }
    That is untested, but its the basic idea

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,066
    Thanks
    2
    Thanked 319 Times in 311 Posts
    Using a foreach() loop on an array eliminates the need to have a counter/index variable and you don't need to keep track of how many elements there are.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    To do what you want with the variables you have and if it will always be static you can do this:

    PHP Code:
    for($i 1$i 51$i++)
    {
        
    $name "Variable".$i;
        if ($
    $name !== "")
        {
            
    $VariableX $VariableX 1;
        } 

    now this isn't the most efficient way of doing it though. Like everyone said using an array is ideal. It is easier and more efficient then a bunch of variables running around. Plus it allows for things to be dynamic. a simple use of count() on the array will give you how many elements are in the array or simple use foreach(). An example of using this to your advantage with what you have is this:

    PHP Code:

    // Lets say you set the variables like so:

    $Variable[] = 'asdf';
    $Variable[] = '';
    $Variable[] = 'asdf';

    // This would be 3 elements which would be 
    // like your old  $Variable1 $Variable2 $Variable3
    // Now we loop through them with foreach()

    foreach($Variable as $v)
    {
        if (
    $v !== "")
        {
            
    //$VariableX = $VariableX + 1; Lets change this
            // This is assuming you already set $VariableX 
            // to 0 higher up
            
    $VariableX++;
        } 
    }

    // The loop would result in $VariableX equalling 2
    // Notice I also changed how it added $VariableX 
    // because its cleaner :P 


  •  

    Posting Permissions

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