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

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,206 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