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) Adding a Basic Form Checking the Request Method

what is isset?

help me to understand this isset method.

3 Answers

György Varga
György Varga
19,198 Points


isset is returning with true if the variable you add to isset ( for example: isset($var) ) is set to anything before, if not it, will return with false.

Hope this helps!

Hi there, I'll just add to Varga's answer. isset is good to use a lot of times if you're checking variables after a form has been submitted and there is either an error and the user needs to fix a value they entered (i.e. First Name has numbers in it), or they filled everything out correctly and you return them to a page and post a message saying 'Error found' or 'Success!' --- on that page you would have a variable maybe called $errorMessage or $successMessage that would only show if it was set by those 2 previous actions I mentioned.

Here's a common little code snippet of code on a page where users fill out a form that is checked when they submit it:

//Used to display a message if the user incorrectly filled out part of a form that has been submitted
if(isset($errorMessage)) { echo $errorMessage; }

Varga's got the concept right - if a variable has a value that PHP considers not null, isset will return true. Otherwise, if the variable has not been initialized or the value is NULL, isset will return false.

I hope this makes any sense at all!