Home  >  Article  >  Backend Development  >  Similarities and differences between isset() and !empty() functions in PHP

Similarities and differences between isset() and !empty() functions in PHP

autoload
autoloadOriginal
2021-03-26 14:14:172806browse

isset() and ! The empty() function is similar, both will return the same result. But the only difference is! The empty() function does not generate any warnings or electronic notifications when the variable does not exist. It's enough to use either function. By merging two functions into a program it causes time lapse and unnecessary memory usage.

1.isset()

isset ( mixed $var , mixed $... = ? ) : bool
  • var: The variable to be checked.

  • Return value: If var exists and the value is not null, return true, otherwise return false.

PS: If a variable has been released using unset(), it will no longer be isset(). If isset() is used to test a variable that is set to null, false will be returned. At the same time, it should be noted that the null character ("\0") is not equivalent to the null constant of PHP. If multiple parameters are passed in at one time, then isset() will only return when all parameters are set true The calculation process is from left to right, and when an unset variable is encountered midway will stop immediately.

<?php 
  
  $num = &#39;0&#39;; 
  if( isset( $num ) ) 
  { 
      print_r(" $num is set with isset  ");
   } 
   echo "<br>";
// 声明一个空数组 $array = array(); 
  echo isset($array[&#39;geeks&#39;]) ? &#39;array is set.&#39; : &#39;array is not set.&#39;; 
?>

Output:

0 is set with isset functionarray is not set.
array is not set.


##2.empty()

empty ( mixed $var ) : bool

  • var: Variable to be checked

  • Return value: Return when var exists and is a non-empty and non-zero value

    false Otherwise return true.

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

0 is considered empty
1 is considered set

The following content will be judged as empty:

  • "" (empty String)

  • 0 (0 as an integer)

  • 0.0 (0 as a floating point number)

  • "0" (0 as a string)

  • null

  • ##fals
  • earray() (an empty array)
  • $var; (a variable declared but without a value)

3. Similarities and differences between the two isset() and

! The empty()

function is similar, both will return the same result. But the only difference is! The empty() function does not generate any warnings or electronic notifications when the variable does not exist. It's enough to use either function. By merging two functions into a program it causes time lapse and unnecessary memory usage. <pre class="brush:php;toolbar:false">&lt;?php $num = &amp;#39;0&amp;#39;; if( isset ( $num ) ) { print_r( $num . &quot; is set with isset function&quot;); } echo &quot;\n&quot;; $num = 1; if( !empty ( $num ) ) { print_r($num . &quot; is set with !empty function&quot;); } ?&gt;</pre><pre class="brush:php;toolbar:false">0 is set with isset function 1 is set with !empty function</pre> Recommended: "

php video tutorial

" "php tutorial"

The above is the detailed content of Similarities and differences between isset() and !empty() functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn