php basics
PHP isset
PHP isset is used to check whether a variable is set, and in addition to checking the variable , isset can also check the elements of the array ( PHP Array ), which is a very practical function. If you use isset to check a variable that is set to NULL, it will return false, but if the variable is a string NULL, it will return true.
PHP isset syntax example
bool isset (variable or array to be checked);
isset is often used with the unset function to check whether the result of unset meets the requirements. As mentioned earlier, in addition to checking variables , isset can also check array elements, so two implementation examples are prepared below for your reference.1. PHP isset implementation example: check general variables
<?php
$str1="Test string 1";
$str2="Test string 2";
echo isset($str1); // TRUE
echo isset($str2); // TRUE
echo isset($str1,$ str2); // TRUE
unset($str2); // Clear the value of $str2
echo isset($str1); // TRUE
echo isset($str2); // FALSE
echo isset($str1,$str2); / / FALSE
?>
At the beginning of the example, two string variables are prepared , and then are respectively checked through isset to check whether there are settings. Special attention is paid to the third check we used isset($str1,$str2); this way of writing to check two at a time Value, this is allowed, then clear the value of the variable $str2 through unset , and check the result again. 1. PHP isset implementation example: check array elements$str1="Test string 1";
$str2="Test string 2";
echo isset($str1); // TRUE
echo isset($str2); // TRUE
echo isset($str1,$ str2); // TRUE
unset($str2); // Clear the value of $str2
echo isset($str1); // TRUE
echo isset($str2); // FALSE
echo isset($str1,$str2); / / FALSE
?>
<?php
$Arr = array ('A' => '1', 'B' => 'null' ,'C' => null);
echo isset($Arr['A']); // TRUE
echo isset($Arr['B']); // TRUE
echo isset($Arr['C']); // FALSE
?>
The array $Arr in this example has three elements. The first two element values are strings , and the last element value is null. Then we use isset to determine whether there is a setting, and it turns out that the first two array values are both set. And the last $Arr['c'] returns FALSE.$Arr = array ('A' => '1', 'B' => 'null' ,'C' => null);
echo isset($Arr['A']); // TRUE
echo isset($Arr['B']); // TRUE
echo isset($Arr['C']); // FALSE
?>
PS. PHP 5.4.0 and later versions will return FALSE when checking the non-numeric offset of a character.
Post a Comment
0 Comments