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

WBOY
풀어 주다: 2016-06-13 12:23:22
원래의
760명이 탐색했습니다.

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>
로그인 후 복사
<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在判断变量没什么作用,但是在判断数组有无一个变量却很好用:

<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不了解的可以去官网看一下就明白了:
http://php.net/manual/zh/function.isset.php

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿