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

autoload
Release: 2023-04-09 20:34:02
Original
2675 people have browsed it

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
Copy after login
  • 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;; 
?>
Copy after login

Output:

0 is set with isset functionarray is not set.
array is not set.
Copy after login


##2.empty()

empty ( mixed $var ) : bool
Copy after login

  • 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;;
       } 
      ?>
    Copy after login
Output

0 is considered empty
1 is considered set
Copy after login

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.

<?php 
 
$num = &#39;0&#39;; 
  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");
   }
 ?>
Copy after login
0 is set with isset function
1 is set with !empty function
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!