SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: While Loop

  1. #1
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    While Loop

    Hi,

    I dont know the syntax for a while loop that runs while two conditions are met.

    i.e.

    while ($my_array != array(-1, -1, -1) AND $my_array_2 != array(-1, -1, -1)) {
    }

    am i using the correct syntax here? I need it to run until both arguments are true.

    Thanks

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes.
    Saul

  3. #3
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, for some reason it only seems to be running until one of the arguments is true.

    If you run this script:

    $my_array = array(1, 10, 15);
    $my_array_2 = array(1, 10, 15);

    while ($my_array != array(-1, -1, -1) AND $my_array_2 != array(-1, -1, -1)) {

    $fixture_1 = $my_array[rand(0, 2)];
    $fixture_2 = $my_array_2[rand(0, 2)];

    echo "fixture 1 = ".$fixture_1."<br>";
    echo "fixture 2 = ".$fixture_2."<br><br>";

    echo "my array 1-0= ".$my_array[0]."<br>";
    echo "my array 1-1 = ".$my_array[1]."<br>";
    echo "my array 1-2 = ".$my_array[2]."<br><br>";

    echo "my array 2-0= ".$my_array_2[0]."<br>";
    echo "my array 2-1 = ".$my_array_2[1]."<br>";
    echo "my array 2-2 = ".$my_array_2[2]."<br><br><br>";
    echo "-----------------------------------------<br><br>";

    if ($fixture_1 == 1) {
    $my_array[0] = -1;
    }
    if ($fixture_1 == 10) {
    $my_array[1] = -1;
    }
    if ($fixture_1 == 15) {
    $my_array[2] = -1;
    }

    if ($fixture_2 == 1) {
    $my_array_2[0] = -1;
    }
    if ($fixture_2 == 10) {
    $my_array_2[1] = -1;
    }
    if ($fixture_2 == 15) {
    $my_array_2[2] = -1;
    }
    }

    It runs until one of the arrays have changed to [-1,-1, -1]

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So you wanna use OR.
    Saul

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It runs until one of the arrays have changed to [-1,-1, -1]
    When one of the array changes to [-1, -1, -1], one of the condition in while structure becomes false and loop terminates.

  6. #6
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, the Or seems to be doing it. Im only a noob learning how to do array


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
  •