Difference between PHP if $variable, isset(), empty(), strlen()

What is the difference between PHP functions below ?

if ($variable) { }
if (isset($variable)) { }
if (!empty($variable)) { }
if (strlen($variable)) { }
posted on 23.05.2020 at 18:30
0pnshow more
onefiveonefive
Hi,

The differences between those PHP functions are visible in specific cases. For example when variable is created, is NULL, is 0, is TRUE or FALSE etc.

In the condition if ($variable) { echo "OK"; } the output will be OK when $variable:
  • is not empty
  • is TRUE
  • is not FALSE
  • is not 0
  • is not NULL






In the condition if (isset($variable)) { echo "OK"; } the output will be OK when $variable:
  • is set and is different from NULL


In the condition if (!empty($variable)) { echo "OK"; } the output will be OK when $variable:
  • is not empty
  • is TRUE
  • is not FALSE
  • is not 0
  • is not NULL






In the condition if (strlen($variable)) { echo "OK"; } the output will be OK when $variable:
  • is not empty
  • is TRUE
  • is not FALSE
  • is not NULL





In relation to these functions, you can use is_null() to find out if variable is NULL.
posted on 24.05.2020 at 12:24
0pnshow more
TimTim
share on facebookshare on twitter
2020 AnswerTabsTermsContact us
This site uses cookies to improve your experience, for analytics and ads. By continuing to browse this site you agree to our use of cookies.