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 trialIvan Tomicic
3,873 PointsWhat if there is no variable in page?
Ok I tried to work on this task and if I don't set any variable to index.php browser gives me an error, how can I tell it just not to show anything in code if there is no variable? I tried with else function but no luck?
1 Answer
miguelcastro2
Courses Plus Student 6,573 Points// Check to see if a variable is set
if (isset($my_variable)) {
echo 'Set';
} else {
echo 'Not Set';
}
Ivan Tomicic
3,873 PointsIvan Tomicic
3,873 PointsOk, but I need to check for variable too, is there a shorter way than this
miguelcastro2
Courses Plus Student 6,573 Pointsmiguelcastro2
Courses Plus Student 6,573 PointsIn this IF clause, if the first condition is TRUE, meaning that the variable $section is set, then it will check the second condition to see if $section == "shirts".
Ivan Tomicic
3,873 PointsIvan Tomicic
3,873 PointsThank you!
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Ivan,
This was the code I used if you want to compare to your own:
There's no need to have the else block where you echo out an empty string.
Ivan Tomicic
3,873 PointsIvan Tomicic
3,873 PointsOh, thank you, that is useful :)