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 trialAmmu Nair
9,100 PointsSort - Clarification required in stmt combining two if's.
Hi, I need some help in understanding the stmt where the 2 if stmts are combined. They were given separately so that the catalogs could be viewed as per category. If it is combined using an OR, how will the catalogs be displayed as per category ? In fact, if they are combined using an OR, there isnt any need of an IF stmt at all, as it will always be true. Please explain.
Ammu Nair
9,100 PointsSure, The link is below: https://teamtreehouse.com/library/build-a-basic-php-website/listing-and-sorting-inventory-items/sorting-array-items
- from the course 'buiding basic website in php'. Thanks
1 Answer
Simon Coates
28,694 Pointsif ($category == null OR strtolower($category) == strtolower($item["category"])) {
if category is not null and the categories doesn't match, it will not be true. you can past conditions together using an OR when the same action occurs if either (or both) conditions are met. It is possible to accidentally create a effective tautology (or a contradiction), but here there is another case - !($category == null OR strtolower($category) == strtolower($item["category"])) evaluates to ($category != null AND strtolower($category) != strtolower($item["category"])). If there are two possible outcomes, an if statement still makes sense.
Ammu Nair
9,100 PointsHi, If the category is 'books', we want only the items with category = 'books' to be displayed. Could you please explain how using this code, the items with only category = 'books' are displayed.
Simon Coates
28,694 PointsIt tests category == null. this is not true so it looks at the next part. The category 'books' equals the category on some items. False OR true - is true.
You can demo the test:
<?php
$item = [ "category"=>"Books" ];
$category = 'books';
if ($category == null OR strtolower($category) == strtolower($item["category"])) {
echo "a";
}
$category = 'null';
if ($category == null OR strtolower($category) == strtolower($item["category"])) {
echo "b";
}
$category = 'Movies';
if ($category == null OR strtolower($category) == strtolower($item["category"])) {
echo "c";
}
?>
Only "a" is displayed
Simon Coates
28,694 PointsSimon Coates
28,694 Pointscan you post the code, or a link to the video that this is in reference to?