PHP function isset() can only be used with variables_PHP tutorial

WBOY
Release: 2016-07-15 13:34:26
Original
835 people have browsed it

By pairing

Format: bool isset ( mixed var [, mixed var [, ...]] )

Function: Detect whether the variable Set

Return value:

Returns FALSE if the variable does not exist
If the variable exists and its value is NULL, it also returns FALSE
If the variable exists and the value is not NULL, Then return TRUE

When checking multiple variables at the same time, TRUE will be returned only when each item meets the previous requirement, otherwise the result will be FALSE

Version: PHP 3, PHP 4, PHP 5

More explanation:

After releasing a variable using unset(), it will no longer be isset().
The PHP function isset() can only be used for variables. Passing any other parameters will cause a parsing error.
To detect whether a constant has been set, use the defined() function.

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>$</span><span class="attribute">var</span><span> = </span><span class="attribute-value">''</span><span>;  </span></li><li class="alt"><span>if (isset($var)) // 空值、0、false<br />的赋值结果均被isset判为 TRUE,所以后边<br />的文本将被打印出来。  </span></li><li><span>print "blank value -</span><span class="tag">></span><span> </span><span class="attribute">isset</span><span> = </span><span class="attribute-value">true</span><span>. ";   </span></span></li>
<li class="alt">
<span>$</span><span class="attribute">var</span><span> = </span><span class="attribute-value">NULL</span><span>;if (!isset($var)) // <br>NULL将被isset判为 FALSE  </span>
</li>
<li>
<span>print "NULL value -</span><span class="tag">></span><span> </span><span class="attribute">isset</span><span> = </span><span class="attribute-value">false</span><span>. "; <br>//以下使用 var_dump 输出 isset() 的返回值。  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">a</span><span> = </span><span class="attribute-value">"test"</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">b</span><span> = </span><span class="attribute-value">"anothertest"</span><span>;  </span>
</li>
<li class="alt"><span>var_dump( isset($a) ); // TRUE  </span></li>
<li><span>var_dump( isset ($a, $b) ); // TRUE   </span></li>
<li class="alt"><span>unset ($a);  </span></li>
<li><span>var_dump( isset ($a) ); // FALSE  </span></li>
<li class="alt"><span>var_dump( isset ($a, $b) ); // FALSE  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

The PHP function isset() is also suitable for checking array elements and object elements. If the array or object instance is not defined, testing the array element/object element will return false.

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>$</span><span class="attribute">a</span><span> = </span><span class="attribute-value">array</span><span> ('test' =</span><span class="tag">></span><span> 1, 'hello' =</span><span class="tag">></span><span> NULL);   </span></span></li>
<li class="alt"><span>var_dump( isset ($a['test']) ); // TRUE  </span></li>
<li><span>var_dump( isset ($a['foo']) ); // FALSE  </span></li>
<li class="alt"><span>var_dump( isset ($a['hello']) ); // FALSE   </span></li>
<li><span>// 键 'hello' 的值等于 NULL,所以被认为是未置值的。  </span></li>
<li class="alt"><span>// 如果想检测 NULL 键值,可以试试下边的方法。  </span></li>
<li><span>var_dump( array_key_exists('hello', $a) ); // TRUE  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

Note: Since this is a language structure rather than a function, the PHP function isset() cannot be called by variable functions.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445983.htmlTechArticlePair format: bool isset ( mixed var [, mixed var [, ...]] ) Function: detection Whether the variable is set to a return value: If the variable does not exist, return FALSE. If the variable exists and its value is NULL, also...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!