Q:

Why isn't this simple PHP Forloop working?

First here's the code:
<?php

$qty = $_GET['qty'];

for($i=0; $i < $qty; $i++)
{
    setcookie('animals', $_COOKIE['animals'].'(lion)', time()+3600);
}

?>

Here's what I'm trying to do:
I want to set the value of the "animals" cookie to "(lion)". The number of instances of "(lion)" that should be in the cookie is determined by the "qty" GET parameter's value. So for example if the pages url is:
http://site.com/script.php?qty=10
then the value of the cookie should be:
(lion)(lion)(lion)(lion)(lion)(lion)(lion)(lion)(lion)(lion)
but now it just sets the value once despite the setcookie function being inside the loop, why isn't it working?

A:

Due to the way that cookies work (the browser needs to send the value back to the server as a part of the page request), you can't read and update the contents of the cookie in a single page load like you're attempting to.
As such, instead of attempting to append values to the variable in the $_COOKIE array, simply use a temporary variable as such:
<?php
    $quantity = intval($_GET['qty']) ? intval($_GET['qty']) : 10;    

    $tempString = '';
    for($loop=0; $loop < $quantity; $loop++) {
        $tempString .= '(lion)';
    }

    setcookie('animals', $tempString, time()+3600);
?>

