Title: if/else inside foreach loop problem
Last modified: August 19, 2016

---

# if/else inside foreach loop problem

 *  Resolved [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/)
 * I’ve set up a foreach loop which works fine, except for some reason a standard
   if/else statement is not working properly. It seems to be returning true for 
   the same variable on each loop.
 *     ```
       if ($results) {
       	        echo "<ul>";
       	        foreach ($results as $o):
       	        	$type = $o->action_type;
       	        	$post = get_post($o->post_id);
       	        	$time = $o->action_time;
       	        	$user = $o->user_id;
   
       	        if ($type = 'lame') {
       	        	$phrase = 'this was lame';
       	        } else {
       	        	$phrase = 'something else';
       	        } ?>	        	
   
       	<div class="floatLeft"><?php user_avatar($user, 50); ?></div>
       	<p><?php echo TimeAgoInWords($time); ?> ago</p>
       	<p><?php user_nickname($user); ?> <?php echo $phrase; ?></p>
   
       	       <?php   endforeach; }
       ```
   
 * Its seems that as long as any value is “lame” it will return true, yet if i just
   echo out the value without the if/else, it will put the actual value.
 * Any ideas?

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1138870)
 *     ```
       if ($type = 'lame') {
       	        	$phrase == 'this was lame';
       	        } else {
       	        	$phrase == 'something else';
       	        } ?>
       ```
   
 * Note the double equals. 🙂
 *  Thread Starter [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1138873)
 * Thanks for the quick reply but that did not work. In fact that just made nothing
   show up.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1138879)
 * Sorry! Right approach. Wrong edits.
 *     ```
       if ($type == 'lame') {
       	        	$phrase = 'this was lame';
       	        } else {
       	        	$phrase = 'something else';
       	        } ?>
       ```
   
 *  Thread Starter [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1138883)
 * Ya that was my first though but that too is not working. Its seems that the if/
   else is searching the entire array and returns true if the value is anywhere 
   in there instead of just on its current loop. But again, like I said, if I simply
   echo out the value, it echo the individual value of that loop.
 * [**moderated–bump removed.** Please refrain from bumping as per [Forum Rules](http://wordpress.org/support/topic/68664?replies=6)]
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139126)
 * Please describe, in words, what you want your code to do.
 *  [jamida](https://wordpress.org/support/users/jamida/)
 * (@jamida)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139133)
 * I’m not familiar with this syntax. Is it valid PHP?
 *     ```
       foreach ($results as $o):
           $type = $o->action_type;
       endforeach;
       ```
   
 * Specifically the “:” at the end of the foreach line and the closing “endforeach;”.
 * If indeed this is the problem then it could be that the closing “:” on the foreach
   line just makes the loop run through all the $results with the effect that $o
   is assigned the last result and then your code runs.
 * Good luck
 *  Thread Starter [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139153)
 * My apologies. Here’s what I’m looking to do. I have a custom table with a specific
   field “action_type” that has 5 or so different possible values, example: ‘vote’,‘
   favorite’, ‘comment’, etc. I’m listing out the actions taken by users chronologically
   by time.
 * For example, if the action_type is ‘vote’ then write “Username has voted on Post
   17”,
    or if action_type is ‘favorite’, then write “Username has added Post 17
   to his favorites” etc.
 * The problem that I’m having is setting up a conditional statement to handle the
   output phrases within the foreach loop. When I test the current iteration for
   the action_type == ‘vote’, it seems to return true if ANY value in my array is‘
   vote’. Because of this, each and every loop iteration returns the same phrase
   as the first value that in the array returned trued.
 * However, like I said, if I simply echo out the action_type, each loop iteration
   will write the appropriate type: ie, ‘vote’ ‘vote’ ‘favorite’ ‘vote’ ‘comment’
   ect.
 * Please let me know if if that makes sense or if I can clarify any part. Thanks
   again!
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139154)
 * After:
    `foreach ($results as $o):`
 * put this to make sure $o has the necessary elements:
 * `echo "<pre>"; print_r($o); echo "</pre>";`
 *  Thread Starter [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139155)
 * Now I take it this is just for testing purposes? This is what it prints on a 
   single loop iteration:
 *     ```
       stdClass Object
       (
           [user_id] => 2
           [post_id] => 131
           [action_type] =>  vote
           [action_time] => 2009-07-15 13:32:06
       )
       ```
   
 * Each loop is returning the proper values.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139158)
 * i am new to this, just a maybe useless question:
    is ‘ vote’ the same as ‘vote’
   i.e. is a preceeding ‘space’ important for the comparison?
 *  [tomphil](https://wordpress.org/support/users/tomphil/)
 * (@tomphil)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139159)
 * Thanks
 *  Thread Starter [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139162)
 * alchymyth, new or not, you are absolutely right! I did indeed accidentally leave
   a space in my variable. Problem solved. Thanks so much. And thanks MichaelH for
   allowing him to find it. Really appreciate it guys!

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘if/else inside foreach loop problem’ is closed to new replies.

## Tags

 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [ifelse](https://wordpress.org/support/topic-tag/ifelse/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [problem](https://wordpress.org/support/topic-tag/problem/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 12 replies
 * 6 participants
 * Last reply from: [echstudios](https://wordpress.org/support/users/echstudios/)
 * Last activity: [16 years, 11 months ago](https://wordpress.org/support/topic/ifelse-inside-foreach-loop-problem/#post-1139162)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
