Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Shelby Sarisky
Shelby Sarisky
6,536 Points

If...Statement?

I have been stumped on this a while now and can’t seem to figure it out. What I am trying to do and what I think I have to do to accomplish this is; concatenate my arrays in an “if statement”.

A little background info: The arrays that I am creating are for individual product pages. The products in the array are in the same category compared to the other products that are in different arrays. Each of the products in this particular array all have the same specs (model, dimensions, includes, etc) but different side information (Array 101, 102 and 103 can be paired with this, 104 and 105 can be paired with this).

This is want I want/ Am trying to do: If the array ID equals, 101, 102 and 103, then echo out this statement. So if it is any one of the first 3 arrays I want my code to display something different than if the user were trying to view 104 or 105. But I can’t seem to figure out the right way to concatenate inside of the if statement.

Here’s my code:

<?php if( $product_id == 101){ echo " <p class='text1'>Some Text</p>" ;
} else { echo""; } ?>

Am I on the right track or do I have to o about this a completely different way? Little help?

3 Answers

I would have an array of just your product arrays that you want to target. Example code:

<?php 
// this array will contain all the ids you want to target
$target = array(
    "101",
    "102",
    "103"
);

// check to see if our product id matches one of targeted ids
if (in_array($target, $product_id)) {
    echo "Some Text";
} else {
    echo "";
}
?>
Patrick Cooney
Patrick Cooney
12,216 Points

This way seems unnecessary currently. However if you know you will be adding a lot more products for which you may want to display the same text these show, this is a better route than the code I posted.

Patrick Cooney
Patrick Cooney
12,216 Points
<?php

if( $product_id == 101 || $product_id == 102 || $product_id == 103){
 echo " Some Text" ;
} else {
 echo""; 
} 

?>

I recommend this approach due to its ease. That way when you come back to it you don't have to dig through your code to find the array holding the options.

Shelby Sarisky
Shelby Sarisky
6,536 Points

Thank you! That works perfectly! I knew there was a way to do this, just could not think about how to go about it. Thanks again!

Patrick Cooney
Patrick Cooney
12,216 Points

Just to make sure you understand what we're doing, the || means "or". There is also the "and" && operator which will check to see if all of them are true. So currently you are checking if any of the products are specified whereas if you went with the && you're checking if all of the products are specified.

Shelby Sarisky
Shelby Sarisky
6,536 Points

Awesome, makes sense. I will definitely remember that since I had such trouble with this.