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

WBOY
Libérer: 2016-06-13 12:23:22
original
763 Les gens l'ont consulté

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>
Copier après la connexion
<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>
Copier après la connexion
<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>
Copier après la connexion

只有在变量为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>
Copier après la connexion

虽然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>
Copier après la connexion

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

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal