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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.