Why use isset ?

+5 Erp Eight · November 24, 2014
I am learning php and i have no idea why we use isset like this
if(isset($_get or post['anyusername']))
i know isset is used to check if a varible has been set.

Post a Reply


Oldest  Newest  Rating
+1 Eng Iroqious Saviour · November 24, 2014
The isset () function is used to check whether a variable is set or not. If a variable is already unset with unset() function, it will no longer be set.
0 Jason Blesing · November 24, 2014
Hi. When you start to use databases, some data might be placed into a variable. If for some reason, it hasn't, then isset will cancel whatever operation you are trying to achieve.

I'm a ruby on rails programmer, so sorry if my code looks weird

$data = Data.all;

In other page(If $data was global), I'm echoing the first object in the database:

if isset($data)
echo $data.first;

+3 Ron Butcher · November 25, 2014
When you are working with variables that are assigned through POST or GET you always want to make sure that they exist before working with them.  If you try to work with a variable that does not exist, the page will crash and an error will occur.

By using the isset() function, you only perform the code to work with the variable if it actually exists.  So if it does not, the page will skip that part and continue instead of crashing.  This is really to cover your ass since most of your users will do everything that they can to break your page.  You have to assume the user is not that intelligent.

if(isset($_POST['username']) && isset($_POST['password']))
$username = $_POST['username'];
$password = $_POST['password'];

logInUser($username, $password);
echo 'You did not properly submit a form. Please try again.';

This is especially important when dealing with GET variables.  Since GET is part of the URL, the user can change and modify them however they please.
+1 Erp Eight · November 25, 2014
Okay i understand isset
but what does this code means in plain english
0 Ron Butcher · November 25, 2014
It means, if the variable $_POST['username'] has been created, do this....

Do you understand how $_POST and $_GET work?  They are arrays with variables that have been carried in from another page, usually submitted by a form that the user filled out an pressed the 'submit' button.  Because they were carried in from another page, and not created by your code specifically, you need to make sure that they exist before working with them.
+1 Erp Eight · December 11, 2014
Then how can we use isset to check that user has entered value?
+2 Ron Butcher · December 11, 2014
You would use the empty() function.  I'll give you an example.  I have a search form that will let me search a part of my database for something.  This is how my code would look on the page the form would go to:
//  First we check to see if the form was submitted, or if the page is being visited without a proper
// form being submitted
// Next we make sure that the variable actually has a value and the user did not just submit
// an empty form
echo 'You are searching for '.$_GET['searchFor'];
echo 'You did not enter anything to search for.';

You can check to see if it is set and empty in the same if statement, but this breaks down the step by step.
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.