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

What isset() function exactly do in PHP? Is it really necessary to use in the "Build a Basic PHP Website" project?

What isset() function exactly do in PHP? Is it really necessary to use in the "Build a Basic PHP Website" project?

Hi everyone,

I'm currently doing the "Build a Basic PHP Website" course and I'm seeing Alena use the isset function, she says that we need to use it to do another check? Why do we need to do another check? I'm trying out the code without the isset() function and it still works fine the way it is, what's the difference?

1). What is it exactly used for?

2). Do you have to use it?

3). When is it good practice to use it?

Thanks!

3 Answers

Casey Ydenberg
Casey Ydenberg
15,622 Points

Generally it's a good idea to use it if there's any doubt as to whether a value will exist or not. So if for example you are processing POST data, and accessing $_POST['some_value'], you can never really be sure whether that data will be set. Accessing directly will cause an error, and might cause your page to fail outright. isset() returns true if the data exists, false if it does not, so that you can guard against this error being thrown.

I guess in answer to your other two questions, you don't HAVE to use it in the case above, but not doing so results in cases where one part of your code makes assumptions about another part of your code (ie a function that accepts an array makes assumptions about the code that calls the function, or code that processes a form makes assumptions about the form itself, to say nothing of - ahem - "users" who try to submit requests without the form).

This is something to avoid, you want to make your program as loosely coupled as possible, creating a lower overhead to to making changes down the road.

My question is: instead of using isset(), why can't I just use a default else statement, like this:

if ($_GET["cat"] == "books") {
    $pageTitle = "Books";
} elseif ($_GET["cat"] == "movies") {
    $pageTitle = "Movies";
} elseif ($_GET["cat"] == "music") {
    $pageTitle = "Music";
} else {
    $pageTitle = "Full Catalog";
}
Casey Ydenberg
Casey Ydenberg
15,622 Points

You can do that, but the first $_GET["cat"] will trigger a warning and/or error.

I tried the else thing, but there is an error

Notice: Undefined index: cat in /home/treehouse/workspace/catalog.php on line 6

Notice: Undefined index: cat in /home/treehouse/workspace/catalog.php on line 8

Notice: Undefined index: cat in /home/treehouse/workspace/catalog.php on line 10

見綸 陳
見綸 陳
6,333 Points

Check this --> http://php.net/manual/zh/function.isset.php

I'm a php beginner. I think we can add more code like this?

if(isset($_GET["cat"])) {

    if($_GET["cat"] == "page_1") { 
        $pageTitle = "page_1";
    } else if ($_GET["cat"] == "page_2") {
        $pageTitle = "page_2";
    } else if ($_GET["cat"] == "page_3") {
        $pageTitle = "page_3";
    } else { 
        $pageTitle = "All pages";
    }

} else { 

    $pageTitle = "Error!!! Pages not fund";

}