Why doesn't my foreach loop work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Why doesn't my foreach loop work?

Post by Chris Corbyn »

Hi,

Been looking at this for ages but I can't figure out why it doesn't work:

Code: Select all

$collective = array ('man', 'cat', 'water');

foreach ($collective as $n) {

    if ($n == 'cat') {
        $_SESSION['val1'] = 'cat';
        echo 'Success! '.$n.' found';
        echo '<a href="doit.php? '. SID .' ">Do This</a>';
        exit();
    } else {
        echo 'not found';
        exit();
    }
I just get the else statement each time except for when I change:

Code: Select all

if ($n == 'cat') {
to

Code: Select all

if ($n == 'man') {
Then that one works. It's as if it'll only read the first element in my array???

Where am I going wrong?
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Code: Select all

$collective = array ('man', 'cat', 'water');

foreach ($collective as $n) {

    if ($n == 'cat') {
        $_SESSION['val1'] = 'cat';
        echo 'Success! '.$n.' found';
        echo '<a href="doit.php? '. $SID .' ">Do This</a>'; #missing $ sign
        exit();
    } else {
        echo 'not found';
        exit();
    }
} #missing end if
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

exit() stop's execution of all scripts (on this request)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

still doesn't work. Sorry never noticed the missing brace and $ they were there in the actual script. Would exit(); stop execution in a foreach loop since I thought it worked like this...

Look to see IF first element == cat, then look to see if second element == cat, then look to see if third element == cat. And only once it's done the entire loop would it move to the ELSE statement. And only when it find element which is == cat will it run the IF statement.

Tried taking all the exits out but then I get this output on the page
notfound Success! cat found Do This! not found
which made me realise that I shouldn't be using a foreach loop. I only want it to serach the array for the word cat not run the script foreach one. I beleive there's a function to do that. I'll take alook thanks :-)

Thanks.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

in_array() will probably do your job
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah that woked. Thanks :-)
Post Reply