<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Determine if a variable is set and is not NULL</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.is-string.html">is_string</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 if a variable is set and is not <strong><code>NULL</code></strong></span></p> </div> <div class="refsect1 description" id="refsect1-function.isset-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>isset</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span> ] )</div> <p class="para rdfs-comment"> Determine if a variable is set and is not <strong><code>NULL</code></strong>. </p> <p class="para"> If a variable has been unset with <span class="function"><a href="function.unset.html" class="function">unset()</a></span>, it will no longer be set. <span class="function"><strong>isset()</strong></span> will return <strong><code>FALSE</code></strong> if testing a variable that has been set to <strong><code>NULL</code></strong>. Also note that a <strong><code>NULL</code></strong> byte (<em>"\0"</em>) is not equivalent to the PHP <strong><code>NULL</code></strong> constant. </p> <p class="para"> If multiple parameters are supplied then <span class="function"><strong>isset()</strong></span> will return <strong><code>TRUE</code></strong> 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" id="refsect1-function.isset-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">var</code></em></span> <dd> <p class="para"> The variable to be checked. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">...</code></em></span> <dd> <p class="para"> Another variable ... </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.isset-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if <em><code class="parameter">var</code></em> exists and has value other than <strong><code>NULL</code></strong>, <strong><code>FALSE</code></strong> otherwise. </p> </div> <div class="refsect1 changelog" id="refsect1-function.isset-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.4.0</td> <td> <p class="para"> Checking non-numeric offsets of strings now returns <strong><code>FALSE</code></strong>. </p> </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.isset-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5267"> <p><strong>Example #1 <span class="function"><strong>isset()</strong></span> Examples</strong></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">, </span><span style="color: #DD0000">'pie' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'a' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'apple'</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 />// Checking deeper array values<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">'pie'</span><span style="color: #007700">][</span><span style="color: #DD0000">'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: #DD0000">'pie'</span><span style="color: #007700">][</span><span style="color: #DD0000">'b'</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">'cake'</span><span style="color: #007700">][</span><span style="color: #DD0000">'a'</span><span style="color: #007700">][</span><span style="color: #DD0000">'b'</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> <div class="example" id="example-5268"> <p><strong>Example #2 <span class="function"><strong>isset()</strong></span> on String Offsets</strong></p> <div class="example-contents"><p> PHP 5.4 changes how <span class="function"><strong>isset()</strong></span> behaves when passed string offsets. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$expected_array_got_string </span><span style="color: #007700">= </span><span style="color: #DD0000">'somestring'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'some_key'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0.5</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0.5'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0 Mostel'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>Output of the above example in PHP 5.3:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) </pre></div> </div> <div class="example-contents"><p>Output of the above example in PHP 5.4:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> bool(false) bool(true) bool(true) bool(true) bool(false) bool(false) </pre></div> </div> </div> </div> <div class="refsect1 notes" id="refsect1-function.isset-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> <span class="function"><strong>isset()</strong></span> 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 <span class="function"><a href="function.defined.html" class="function">defined()</a></span> function. </p> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <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> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> When using <span class="function"><strong>isset()</strong></span> on inaccessible object properties, the <a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a> overloading method will be called, if declared. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.isset-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.empty.html" class="function" rel="rdfs-seeAlso">empty()</a> - Determine whether a variable is empty</span></li> <li class="member"><a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a></li> <li class="member"> <span class="function"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - Unset a given variable</span></li> <li class="member"> <span class="function"><a href="function.defined.html" class="function" rel="rdfs-seeAlso">defined()</a> - Checks whether a given named constant exists</span></li> <li class="member"><a href="types.comparisons.html" class="link">the type comparison tables</a></li> <li class="member"> <span class="function"><a href="function.array-key-exists.html" class="function" rel="rdfs-seeAlso">array_key_exists()</a> - Checks if the given key or index exists in the array</span></li> <li class="member"> <span class="function"><a href="function.is-null.html" class="function" rel="rdfs-seeAlso">is_null()</a> - Finds whether a variable is NULL</span></li> <li class="member">the error control <a href="language.operators.errorcontrol.html" class="link">@</a> operator</li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.is-string.html">is_string</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>