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

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

isset()and! The empty()function is similar, both will return the same result. But the only difference is! Theempty()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 usingunset(), it will no longer be isset(). Ifisset()is used to test a variable that is set to null, false will be returned. At the same time, it should be noted that thenullcharacter ("\0") is not equivalent to the null constant ofPHP. If multiple parameters are passed in at one time, thenisset()will only return when all parameters are settrueThe calculation process is from left to right, and when an unset variable is encountered midway will stop immediately.

"; // 声明一个空数组 $array = array(); echo isset($array['geeks']) ? 'array is set.' : 'array is not set.'; ?>
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

    falseOtherwise returntrue.

  • 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 twoisset() and

! The empty()

function is similar, both will return the same result. But the only difference is! Theempty()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.

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
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!