在本篇文章里小編給大家總結(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ǔ)變量的值。
例:
- <?php
- $num = '0';
- if( isset( $num ) ) {
- print_r(" $num is set with isset function <br>");
- }
- // 聲明一個(gè)空數(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ù)是一種語言構(gòu)造,用于確定給定變量是空還是NULL。!empty()函數(shù)是empty()函數(shù)的否定或補(bǔ)充。empty()函數(shù)與!isset()函數(shù)相當(dāng),而!empty()函數(shù)等于isset()函數(shù)。
例:
- <?php
- $temp = 0;
- if (emptyempty($temp)) {
- echo $temp . ' is considered empty';
- }
- echo "/n";
- $new = 1;
- if (!emptyempty($new)) {
- echo $new . ' is considered set';
- }
- ?>
輸出:
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)存使用。
例:
- <?php
- $num = '0';
- if( isset ( $num ) ) {
- print_r( $num . " is set with isset function");
- }
- echo "/n";
- $num = 1;
- if( !emptyempty ( $num ) ) {
- print_r($num . " is set with !empty function");
- }
輸出:
0 is set with isset function
1 is set with !empty function
新聞熱點(diǎn)
疑難解答