ainternet73
ainternet73

Reputation: 117

Usage of "isset" function

Although I visit php documentation , I didn't understand usage of "isset" function . 1 - for example in a php tutorial book author wrote a text with this context : when we create a form in a first.html and we want to use from form information in second.php , we must use these 2 pieces of code :

if(!isset($_GET['q']))  
       die("The Search key word is not set !!!"); 
$key = $_GET['q'];  
if($key == "")  
       die("The Search key word must be entered !!!"); 

but I don't understand what is difference between these 2 codes ?

2 - for another example I saw this code for checking that a bottom is clicked or not :

if(isset($_POST['login_btn']))  
{  
   ....
}

but I don't understand why does it check that a bottom is clicked or not ?

Upvotes: 3

Views: 7014

Answers (7)

Jay Merritt
Jay Merritt

Reputation: 16

Everyone here has already explained to you what the isset() function checks for (that being if a variable is set or not), but I think what you're really asking about is how $_GET[] and $_POST[] work. You're going to need to look more at the form that feeds this code. Read about what $_GET[] and $_POST[] variables are and I think this code will make a lot more sense to you.

For instance, your second example checks to see if the value named login_btn was sent via post method. If it was, then it runs the ... code block.

Upvotes: 0

mattcan
mattcan

Reputation: 540

I'm just going to dissect your code a little bit..

isset

if(!isset($_GET['q']))  
   die("The Search key word is not set !!!"); 

Would typically be used to see if a variable in a URL is set so something like this:

http://mysite.com/index.php?q=1

Would have $_GET['q'] set and isset($_GET['q']) would come back true

$key==""

$key = $_GET['q'];  
if($key == "")

Would check to see if $key is empty. Using my previous example URL, $key would not be empty or blank, it would be 1.

Why check for a button press

The script that processes your form needs to know that it is being accessed after the form that way it does not error out. This is where you would want to make sure that the form submit button was pressed. As this is confusing, here is an example:

Say you want to insert a tag for your blogging system into a database you might have code that looks like this:

AddTag.php

<form name="addtag" method="process.php" action="post">
    <input type="text" name="tagname" />
    <input type="submit" name="submittag" />
</form>

process.php

<?php
 if ($_POST['submittag'])    {
     //INSERT query
 }
?>

As you can see, if process.php is accessed without the AddTag form being accessed first, the script would not try to insert your tag.

Upvotes: 0

adlawson
adlawson

Reputation: 6431

$key == "";    // Is $key an empty string
isset($key);   // Has the $key variable been defined and not null

And just for reference, here are a few others that may be useful

empty($key);   // Is $key and empty value (0, false, null, "", array())
$key == false  // Is $key a falsey value (0, false, null, "", "0", array())
$key === false // Is $key a false boolean (false)

Upvotes: 0

Metalmini
Metalmini

Reputation: 111

This function is quite simple

As on php.net:

Determine if a variable is set and is not NULL.

In your case, the isset function checks of your post variable is empty or not

Upvotes: 0

wanovak
wanovak

Reputation: 6127

isset checks to see if a given variable or index exists. If you assume that a variable exists and it doesn't, PHP generates a notice. All you're doing by using isset is suppressing possible notices -- which is good practice.

Upvotes: 2

Brad
Brad

Reputation: 163232

$key="";
isset($key)  //This evaluates to true. 

The string is empty, but the variable is defined. The isset() function returns true, even for an empty string.

Perhaps you would like to use empty() instead, which has a broader range of conditions that evaluate to false.

Upvotes: 2

dqhendricks
dqhendricks

Reputation: 19251

The difference is, in one instance the form was not submitted, in the second instance, the form was submitted, but with a blank value.

You wouldn't want to process a form on your page if the form has not been submitted yet.

There may also be multiple forms on one page, so you might check the button value to find out which form was submitted.

Upvotes: 1

Related Questions