Jump to content

difference between !isset and empty


ivytony

Recommended Posts

From my understanding, [pre]isset[/pre] checks if the variable is even defined.

 

 

[pre]empty[/pre] checks if it is one of a few values. From the PHP website:

 

empty — Determine whether a variable is empty

 

Returns FALSE if var  has a non-empty and non-zero value.

 

The following things are considered to be empty:

 

    * "" (an empty string)

    * 0 (0 as an integer)

    * "0" (0 as a string)

    * NULL

    * FALSE

    * array() (an empty array)

    * var $var; (a variable declared, but without a value in a class)

This should help you understand.

 


<?php

$a = FALSE;
$b = "";
$c = 0;
$d = 'anything';

if (!isset($a) )
  echo '$a is not set<br>';
if (empty($a) )
  echo '$a is empty<br>';

if (!isset($b) )
  echo '$b is not set<br>';
if (empty($b) )
  echo '$b is empty<br>';

if (!isset($c) )
  echo '$c is not set<br>';
if (empty($c) )
  echo '$c is empty<br>';

if (!isset($d) )
  echo '$d is not set<br>';
if (empty($d) )
  echo '$d is empty<br>';

if (!isset($e) )
  echo '$e is not set<br>';
if (empty($e) )
  echo '$e is empty<br>';

?>

 

Returns

 


$a is empty
$b is empty
$c is empty
$e is not set
$e is empty

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.