在php中判断变量为空我们有很多种方法,php为我们提供了empty,isset,var == null,is_null等等函数来进行变量判断,下面我来给大家分享这些方法的使用方法.empty() 与 isset() 的一个简单比较.
实例代码如下:
- <!–?php
- $var = 0; // 结果为 true,因为 $var 为空
- // 结果为 true,因为 $var =0
- if (emptyempty($var)) { echo '$var is either 0 or not set at all'; }
- //结果为false, 因为$var已经设置了
- if (!isset($var)) { echo '$var is not set at all'; }
- ?>
上面的empty与isset判断为没有任何属性的对象都将被认为是空.var == null
功能:判断变量是否为“空”
说明:值为 0 或 false 或 空字符串”" 或 null的变量、空数组、都将判断为 null
注意:与empty的显著不同就是:变量未初始化时 var == null 将会报错.
实例代码如下:
- $a = 0;
- $b = array();
- if ($a == null) echo '$a 为空' . "";
- if ($b == null) echo '$b 为空' . "";
- if ($c == null) echo '$b 为空' . "";
- // 显示结果为
- // $a 为空
- // $b 为空
- // Undefined variable: c
is_null实例代码如下:
- $a = null;
- $b = false;
- if (is_null($a)) echo '$a 为NULL' . "";
- if (is_null($b)) echo '$b 为NULL' . "";
- if (is_null($c)) echo '$c 为NULL' . "";
- // 显示结果为
- // $a 为NULL
- // Undefined variable: c
- 变量==0 或 变量===0
实例代码如下:
- $a ===0 $a ===""
- $a ==0 等于 $a ==""
在php url传参数时php页面的 URL 尾部参数出现 id=0 时(比如:test.php?id=0),试比较:
实例代码如下:
- if(emptyempty($id)) $id=1; // 若 id=0 ,id 也会为1
- if(!isset($id)) $id=1; // 若 id=0 ,id 不会为1
- 可分开运行以下代码检测上述推断:
- if(emptyempty($id)) $id=1;
- print $id; // 得到 1
- if(!isset($id)) $id=1;
- print $id; //得到 0
总结
在php中NULL与空是两种不同的概念,isset 主要用来判断变量是否被初始化过empty 可以将值为 “假”、“空”、“0”、“NULL”、“未初始化” 的变量都判断为TRUE
is_null 仅把值为 “NULL” 的变量判断为TRUE
var == null 把值为 “假”、“空”、“0”、“NULL” 的变量都判断为TRUE
var === null 仅把值为 “NULL” 的变量判断为TRUE
其它一些可能用到php变量判断的函数
isset($ var) : //变量是不是已经声明
empty($ var): //变量是不是为空
defined($ var): //常量是不是已经定义 define()
is_array($ var): //检验测定变量是不是是数组
is_null($ var): //检验测定变量是不是为 null
波比源码 » php 判断变量为空详解介绍
order levofloxacin 250mg sale order levaquin 500mg generic
buy avodart 0.5mg generic order flomax 0.2mg without prescription generic zofran
cheap aldactone buy propecia 1mg online cheap fluconazole medication
acillin canada generic ampicillin erythromycin 500mg sale
buy sildenafil buy tamoxifen pills robaxin price
sildenafil 50mg pill sildenafil pills 50mg brand estradiol 1mg
lamotrigine usa cheap tretinoin gel retin cream ca
order tadalafil 10mg for sale order tadalafil 20mg pills order voltaren generic
isotretinoin pills buy azithromycin sale azithromycin 250mg sale
buy indomethacin 75mg pill order lamisil sale buy amoxicillin for sale
buy tadalafil generic best medication for ed order sildenafil pills
generic arimidex anastrozole 1 mg uk cost viagra
deltasone 40mg tablet order sildenafil 100mg pill buy viagra 50mg
cialis 20mg bestellen viagra 100mg ohne rezept viagra 50mg kaufen für männer
buy isotretinoin 20mg online isotretinoin 20mg tablet stromectol for sale online
doxycycline pill purchase clomiphene buy furosemide 100mg generic
buy ramipril 5mg sale order clobetasol astelin 10ml usa
buspar 5mg drug order oxybutynin 2.5mg for sale ditropan 2.5mg without prescription
terazosin buy online buy actos 15mg generic azulfidine 500 mg canada
imdur 20mg pill imuran sale telmisartan price
zyban pills order quetiapine online quetiapine order
imuran 100 mcg without prescription pantoprazole 20mg pill viagra next day delivery usa
provera pill microzide oral cyproheptadine 4 mg sale
buy luvox 50mg glipizide 10mg without prescription glipizide 10mg brand
isotretinoin online accutane 40mg pills buy prednisone pill
piracetam tablet nootropil us purchase viagra online
brand azithromycin 500mg order gabapentin 600mg pills neurontin over the counter
cialis 5mg cost order sildenafil 50mg pill viagra sildenafil 200mg
usa cialis sales cialis 5mg sale anafranil online buy
aralen generic order olumiant without prescription buy olumiant 4mg generic
order itraconazole 100 mg pills purchase prometrium pills order generic tinidazole 300mg
order glycomet 1000mg online cheap buy atorvastatin 80mg pills tadalafil 10mg over the counter
buy olanzapine 10mg olanzapine 10mg us buy diovan online
norvasc price cialis for sale cialis without prescription
buy omeprazole 20mg generic purchase prilosec pills real poker online
linezolid 600 mg cost zyvox 600mg over the counter slot games
essay helper online blackjack casino playing poker online for money
vardenafil 20mg for sale buy vardenafil without prescription methylprednisolone us
affordable thesis writing purchase sildenafil generic viagra pills 100mg
buy clomiphene generic order generic ventolin 4mg online gambling real money
tadalafil generic name buy cialis pills oral viagra 100mg
order aristocort 10mg order triamcinolone pills buy clarinex 5mg online cheap
order generic tadalafil 40mg viagra oral sildenafil 50mg over the counter
buy orlistat diltiazem for sale order zovirax 400mg online
tadalafil 20mg brand purchase plavix order clopidogrel without prescription
methotrexate price buy methotrexate 2.5mg buy reglan generic
spins real money roulette online free online blackjack real money
buy flomax 0.4mg for sale purchase flomax online cheap order aldactone 100mg pills
cephalexin sale erythromycin 500mg without prescription erythromycin 500mg ca
tadalafil 5mg over the counter tadalafil 20mg for sale viagra usa
sildenafil 100mg cheap sildenafil for men buy tadalafil 5mg pills
order ceftin 250mg order methocarbamol 500mg pills robaxin ca
casino slots free slot games tadalafil 10mg for sale
buy trazodone 100mg pill aurogra medication sildenafil 100mg over the counter