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

WBOY
Lepaskan: 2016-06-13 12:23:22
asal
764 orang telah melayarinya

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>
Salin selepas log masuk
<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>
Salin selepas log masuk
<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>
Salin selepas log masuk

只有在变量为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>
Salin selepas log masuk

虽然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>
Salin selepas log masuk

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

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan