📄 function.isset.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Determine whether a variable is set</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.is-unicode.html">is_unicode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.print-r.html">print_r</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.isset" class="refentry"> <div class="refnamediv"> <h1 class="refname">isset</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">isset</span> — <span class="dc-title">Determine whether a variable is set</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>isset</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$var</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$var</tt></span> [, <span class="methodparam"> <tt class="parameter">$...</tt></span> ]] )</div> <p class="para rdfs-comment"> Determine whether a variable is set. </p> <p class="para"> If a variable has been unset with <a href="function.unset.html" class="function">unset()</a>, it will no longer be set. <b>isset()</b> will return <b><tt>FALSE</tt></b> if testing a variable that has been set to <b><tt>NULL</tt></b>. Also note that a <b><tt>NULL</tt></b> byte (<i>"\0"</i>) is not equivalent to the PHP <b><tt>NULL</tt></b> constant. </p> <p class="para"> If multiple parameters are supplied then <b>isset()</b> will return <b><tt>TRUE</tt></b> only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">var</tt></i></span> <dd> <p class="para"> The variable to be checked. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">var</tt></i></span> <dd> <p class="para"> Another variable .. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">...</tt></i></span> <dd> <p class="para"> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> if <i><tt class="parameter">var</tt></i> exists; <b><tt>FALSE</tt></b> otherwise. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>isset()</b> Examples</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$var </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// This will evaluate to TRUE so the text will be printed.<br /></span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$var</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"This var is set so I will print."</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// In the next examples we'll use var_dump to output<br />// the return value of isset().<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #DD0000">"test"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #DD0000">"anothertest"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)); </span><span style="color: #FF8000">// TRUE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">)); </span><span style="color: #FF8000">// TRUE<br /><br /></span><span style="color: #007700">unset (</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)); </span><span style="color: #FF8000">// FALSE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">)); </span><span style="color: #FF8000">// FALSE<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">)); </span><span style="color: #FF8000">// FALSE<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> This also work for elements in arrays: <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$a </span><span style="color: #007700">= array (</span><span style="color: #DD0000">'test' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'hello' </span><span style="color: #007700">=> </span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'test'</span><span style="color: #007700">])); </span><span style="color: #FF8000">// TRUE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">])); </span><span style="color: #FF8000">// FALSE<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">])); </span><span style="color: #FF8000">// FALSE<br /><br />// The key 'hello' equals NULL so is considered unset<br />// If you want to check for NULL key values then try: <br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">)); </span><span style="color: #FF8000">// TRUE<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <div class="warning"><b class="warning">Warning</b> <p class="para"> <b>isset()</b> only works with variables as passing anything else will result in a parse error. For checking if <a href="language.constants.html" class="link">constants</a> are set use the <a href="function.defined.html" class="function">defined()</a> function. </p> </div> <blockquote><p><b class="note">Note</b>: <span class="simpara">Because this is a language construct and not a function, it cannot be called using <a href="functions.variable-functions.html" class="link">variable functions</a></span></p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.empty.html" class="function" rel="rdfs-seeAlso">empty()</a></li> <li class="member"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a></li> <li class="member"><a href="function.defined.html" class="function" rel="rdfs-seeAlso">defined()</a></li> <li class="member"><a href="types.comparisons.html" class="link">the type comparison tables</a></li> <li class="member"><a href="function.array-key-exists.html" class="function" rel="rdfs-seeAlso">array_key_exists()</a></li> <li class="member"><a href="function.is-null.html" class="function" rel="rdfs-seeAlso">is_null()</a></li> <li class="member">the error control <a href="language.operators.errorcontrol.html" class="link">@</a> operator</li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.is-unicode.html">is_unicode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.print-r.html">print_r</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -