Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Building a Media Library in PHP Working with _GET Variables

Amy Hsieh
Amy Hsieh
6,023 Points

I can't figure out what difference that isset[$_GET()] makes in that video.

<?php
if (isset($_GET["cat"])){
if($_GET["cat"]=="books"){
$pageTitle="Books";
$section = "books";
} else if ($_GET["cat"]=="movies") {
$pageTitle="Movies";
$section = "movies";
} else if ($_GET["cat"]=="music") {
$pageTitle="Music";
$section = "music";
}
}
?>

Without the isset(), the code above makes sense to me. I don't know why we still need a isset () here. At the end of the video, Alina displayed what happened without isset(). Then I didn't get that part at all. Anyone could explain it for me?

1 Answer

Pascal Breitrück
PLUS
Pascal Breitrück
Courses Plus Student 3,205 Points

Hey, you check with the first line of code the the $_GET[‘cat’] and say hey 👋 is this set . If it’s set the code into the if block will executed. What will be the $pageTitle and $section if $_GET[‘cat’] is not set ????

You take isset() to safe your code. And if the $_GET[‘cat’] is not set PHP can ignore 9 lines of code .

I hope this is some food for thought.

Greets Pascal