欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年02月13日 16:11:09   投稿:laozhang  
在本篇文章里小編給大家總結的是關于PHP中同時檢查isset()和!empty()函數(shù)的必要性原因,有需要的朋友們學習下。

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

語法:

bool isset( $var, mixed )

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

例:

<?php 
  
$num = '0'; 
  
if( isset( $num ) ) { 
  print_r(" $num is set with isset function <br>"); 
} 
  
// 聲明一個空數(shù)組 
$array = array(); 
   
echo isset($array['geeks']) ? 
'array is set.' : 'array is not set.'; 
?>

輸出:

0 is set with isset function
array is not set.

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

例:

<?php 
  
  
$temp = 0; 
  
if (empty($temp)) { 
  echo $temp . ' is considered empty'; 
} 
  
echo "\n"; 
  
$new = 1; 
if (!empty($new)) { 
  echo $new . ' is considered set'; 
} 
?>

輸出:

0 is considered empty
1 is considered set

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

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

例:

<?php 
 
$num = '0'; 
  
if( isset ( $num ) ) { 
  print_r( $num . " is set with isset function"); 
} 
  
echo "\n"; 
  
$num = 1; 
  
if( !empty ( $num ) ) { 
  print_r($num . " is set with !empty function"); 
}

輸出:

0 is set with isset function
1 is set with !empty function

以上就是本次介紹的全部知識點,感謝大家對腳本之家的支持。

相關文章

最新評論