开发者

Usage of "isset" function

开发者 https://www.devze.com 2023-04-08 05:24 出处:网络
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.

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(!is开发者_Python百科set($_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 ?


$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.


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.


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.


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


$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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号