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 trialJason Chiu
4,373 PointsWhy do we need isset variable to check if $_Get variable exists before the if statement executes?
What's the purpose of using isset statement with the $_Get variable? Why we need isset when dealing with $_Get, but not with $_POST or $_SERVER veriable? Thanks.
1 Answer
lihaoquan
12,045 PointsHi Jason, the PHP isset() function checks if a variable is SET to a certain value. For example, you make a variable called $color, and set it to NULL like so : $color = null; Now, you use isset() function to check if the $color variable IS SET to a certain value, since NULL is equivalent to NOT SET, then the isset($color); function will return false, which means $color is not set to any value, however, if you set $color to 'green', like so: $color = 'green', now you use isset(color); function, it'll give you true, which means $color IS BEING SET TO A CERTAIN VALUE That value can be BOOLEAN, INT, STRING, etc EXCEPT FOR NULL.
To answer your question, isset() can be used on all GET, POST, AND SERVER variable, it is just used to check if the GET, POST, or SERVER variable is set to a certain value or not. Whether to check it or not is by your choice, there is no a must or mustn't to use isset() on any variable.
lihaoquan
12,045 Pointslihaoquan
12,045 PointsTo add on, even if youre very sure that a variable will be set to something, you shouldnt assume. Assumptions are the greatest poisons to creating secure web applications You should check variables that are important to the web app's decision making and make sure that they are set to correct values.