PHP中isset(变量)跟直接判断变量的区别

WBOY
Freigeben: 2016-06-13 12:23:22
Original
763 Leute haben es durchsucht

PHP中isset(变量)和直接判断变量的区别

在变量为空字符串和布尔值false以及数值0时,isset全部返回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>
Nach dem Login kopieren
<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>
Nach dem Login kopieren
<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>
Nach dem Login kopieren

只有在变量为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>
Nach dem Login kopieren

虽然isset在判断变量没什么作用,但是在判断数组有无一个变量却很好用:

<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>
Nach dem Login kopieren

还有对isset不了解的可以去官网看一下就明白了:
http://php.net/manual/zh/function.isset.php

版权声明:本文为博主原创文章,未经博主允许不得转载。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage