PHP における isset (変数) と変数の直接判定の違い
変数が空の文字列、ブール値 false、値 0 の場合、isset all は true を返し、直接判定はすべて false を返します:
<code class=" hljs php"><span class="hljs-variable">$empty</span>=<span class="hljs-string">''</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$empty</span>)?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//1</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'<br/>'</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-variable">$empty</span>?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//0</span></code>
<code class=" hljs php"><span class="hljs-variable">$empty</span>=<span class="hljs-keyword">false</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$empty</span>)?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//1</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'<br/>'</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-variable">$empty</span>?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//0</span></code>
<code class=" hljs php"><span class="hljs-variable">$empty</span>=<span class="hljs-number">0</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$empty</span>)?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//1</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'<br/>'</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-variable">$empty</span>?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//0</span></code>
変数が null の場合のみ、区別できません:
<code class=" hljs php"><span class="hljs-variable">$empty</span>=<span class="hljs-keyword">null</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$empty</span>)?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//0</span><span class="hljs-keyword">echo</span> <span class="hljs-string">'<br/>'</span>;<span class="hljs-keyword">echo</span> (<span class="hljs-variable">$empty</span>?<span class="hljs-string">'1'</span>:<span class="hljs-number">0</span>);<span class="hljs-comment">//0</span></code>
isset は変数の判定に役割を持ちませんが、配列に 1 つあるかどうかを判断するために使用されます。 変数は非常に便利です:
<code class=" hljs php"><span class="hljs-variable">$arr</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">'1'</span>=><span class="hljs-number">1</span>,<span class="hljs-string">'3'</span>=><span class="hljs-number">3</span>);<span class="hljs-variable">$temp</span>=<span class="hljs-keyword">null</span>;<span class="hljs-comment">//PS:php中数组不同于map,如果没有key而你去取值会引发错误而不是返回null,所以要有对应的判断,先判断再取值</span><span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$arr</span>[<span class="hljs-string">'2'</span>])) {<span class="hljs-comment">//效果等同于array_key_exists('2', $arr),但相对代码短一点</span> <span class="hljs-variable">$temp</span>=<span class="hljs-variable">$arr</span>[<span class="hljs-string">'2'</span>];}<span class="hljs-keyword">echo</span> <span class="hljs-variable">$temp</span>;</code>
isset について知らない場合は、公式 Web サイトにアクセスして理解できます:
http:// php.net/manual/zh/function.isset. php
著作権表示: この記事はブロガーによるオリジナルの記事であり、ブロガーの許可なく複製することはできません。