Home arrow MySQL arrow Null and Empty Strings

Null and Empty Strings

Anyone who has programmed for any length of time has encountered the concepts of null and empty strings. They are not the same, and confusing the two can cause some serious problems. This article deals with these concepts in the context of PHP and MySQL.

  1. Null and Empty Strings
  2. Making Null Safe in PHP
  3. Null and MySQL
  4. Handling Null in MySQL
By: Shikhar Kumar
Rating: starstarstarstarstar / 13
December 02, 2008

print this article



Before I actually start, here's a little quiz. Is null equivalent to "" ? If you said yes, you definitely need to go through this article, and even if you know that "" is an empty string and not equivalent to NULL, you might want to run through the article.

Let's start by talking about this concept in the context of PHP. The definition of null for PHP goes like this: "The special NULL value represents that a variable has no value. NULL is the only possible value of type NULL."

Note: The null type was introduced in PHP 4.

A variable is considered to be NULL if

  • it has been assigned the constant NULL.

  • it has not been set to any value yet.

  • it has been unset().

Furthermore, there is only one value of type NULL, and that is the case-insensitive keyword NULL.


$var = NULL;


Possible cases where you may misunderstand this and run into problems are:

  1. $var is an empty string

$var = "" ;

$z = is_null($var) ;

return $z ; // returns false


$var = '' ;

$z = is_null($var) ;

return $z ; // returns false

  1. $var is an empty array

$var = array() ;

$z = is_null($var) ;

return $z ; // returns false

The cases where null behaves as it is intended to are: 

$var = null ; // case is not a problem - NULL, null, Null ..are all same

$z = is_null($var) ;

return $z ; // returns true

// $var is undefined or unset or uninitialized

$z = is_null($var) ;

return $z ; // returns true

In most cases we are used to displaying the contents of $var in "if" style i.e.,

if ( $var ) { // returns true when var has value and $var is not equal to zero

echo $var ; // or do some processing


This way of coding has a problem: whenever $var has zero value, it will not return true. To escape this predicament, sometimes one uses is_null(), which again causes the problems I already listed above.

>>> More MySQL Articles          >>> More By Shikhar Kumar

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: