When using PHP to write a page program, I often use the variable processing function to determine whether the PHP page tail parameter of a variable value is empty, at the beginning I used to use the empty () function, but found some problems, so instead of the isset () function, the problem is no longer.
As the name suggests, empty () determines whether a variable is "empty", and isset () determines whether a variable has been set. It is this so-called "as the name implies" that I began with some detours: when a variable value is equal to 0 o'clock, empty () will also be set (True), so there will be some surprises. Originally, Empty () and isset (), although they are variable processing functions, are used to determine whether the variables have been configured, they are a certain difference :empty will also detect whether the variable is empty, zero. When a variable value is 0,empty (), the variable equals null, which is equivalent to no setting.
For example, to detect $id variables, when $id = 0 o'clock, using empty () and isset () to detect whether the variable $id has been configured, both will return a different value--empty () do not think that the configuration, isset () can get $id value:
Copy Code code as follows:
$id = 0;
Empty ($id) print "It's empty.":p rint "it's $id."
Result: It ' s empty.
print "<br>";
!isset ($id) print "It's empty.":p rint "it's $id."
Result: It ' s 0.
This means that when we use a variable-handling function, when the variable is likely to have a value of 0, use empty () to be careful, and it's wiser to replace it with Isset.
When the URL tail parameter of a PHP page appears id=0 (for example: test.php?id=0), try to compare:
Copy Code code as follows:
if (empty ($id)) $id = 1; -If id=0, ID will also be 1
if (!isset ($id)) $id = 1; -if id=0, id not be 1
You can run the following code separately to detect the above inference:
Copy Code code as follows:
if (empty ($id)) $id = 1;
Print $id; Get 1
if (!isset ($id)) $id = 1;
Print $id; Get 0