国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP中檢查isset()和!empty()函數(shù)的必要性

2024-09-04 11:42:16
字體:
供稿:網(wǎng)友

在本篇文章里小編給大家總結(jié)的是關(guān)于PHP中同時(shí)檢查isset()和!empty()函數(shù)的必要性原因,有需要的朋友們學(xué)習(xí)下。

isset()函數(shù)是PHP中的內(nèi)置函數(shù),它檢查變量是否已設(shè)置且不為NULL。此函數(shù)還檢查聲明的變量,數(shù)組或數(shù)組鍵是否具有空值,如果是,isset()返回false,它在所有其他可能的情況下返回true。

語法:bool isset( $var, mixed )

參數(shù):此函數(shù)接受多個(gè)參數(shù)。這個(gè)函數(shù)的第一個(gè)參數(shù)是$ var,此參數(shù)用于存儲(chǔ)變量的值。

例:

  1. <?php  
  2.     
  3. $num = '0';  
  4.     
  5. if( isset( $num ) ) {  
  6.   print_r(" $num is set with isset function <br>");  
  7. }  
  8.     
  9. // 聲明一個(gè)空數(shù)組  
  10. $array = array();  
  11.      
  12. echo isset($array['geeks']) ?  
  13. 'array is set.' : 'array is not set.';  
  14. ?> 

輸出:

0 is set with isset function

array is not set.

empty()函數(shù)是一種語言構(gòu)造,用于確定給定變量是空還是NULL。!empty()函數(shù)是empty()函數(shù)的否定或補(bǔ)充。empty()函數(shù)與!isset()函數(shù)相當(dāng),而!empty()函數(shù)等于isset()函數(shù)。

例:

  1. <?php  
  2.     
  3.     
  4. $temp = 0;  
  5.     
  6. if (emptyempty($temp)) {  
  7.   echo $temp . ' is considered empty';  
  8. }  
  9.     
  10. echo "/n";  
  11.     
  12. $new = 1;  
  13. if (!emptyempty($new)) {  
  14.   echo $new . ' is considered set';  
  15. }  
  16. ?> 

輸出:

0 is considered empty

1 is considered set

檢查兩個(gè)函數(shù)的原因:

isset()和!empty()函數(shù)類似,兩者都將返回相同的結(jié)果,但唯一的區(qū)別是!當(dāng)變量不存在時(shí),empty()函數(shù)不會(huì)生成任何警告或電子通知,它足以使用任何一個(gè)功能。通過將兩個(gè)功能合并到程序中會(huì)導(dǎo)致時(shí)間流逝和不必要的內(nèi)存使用。

例:

  1. <?php  
  2.    
  3. $num = '0';  
  4.     
  5. if( isset ( $num ) ) {  
  6.   print_r( $num . " is set with isset function");  
  7. }  
  8.     
  9. echo "/n";  
  10.     
  11. $num = 1;  
  12.     
  13. if( !emptyempty ( $num ) ) {  
  14.   print_r($num . " is set with !empty function");  

輸出:

0 is set with isset function

1 is set with !empty function

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 碌曲县| 峨眉山市| 封丘县| 囊谦县| 长阳| 台东县| 松滋市| 社会| 义乌市| 墨竹工卡县| 南康市| 永福县| 新密市| 余庆县| 泸水县| 江油市| 太康县| 和政县| 丹阳市| 恩平市| 玉屏| 长汀县| 东阿县| 岐山县| 石林| 澄江县| 迭部县| 元阳县| 寿光市| 噶尔县| 蒲江县| 松溪县| 土默特左旗| 汤原县| 鹤岗市| 焦作市| 连云港市| 仙居县| 江源县| 宜兰市| 武功县|