Home arrow MySQL arrow Page 2 - Null and Empty Strings

Making Null Safe in PHP - MySQL

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



A safe way to to handle this problem would be to use a custom isNull function, like so:

function isNull($var) {


if(is_null($var)) {

return true ;

} else if((string)$var == "") { // $var == "" will return true if $var = 0 that's why we need to cast into string

return true ;

} else if(count($var) < 1) { // return true even if its an empty array

return true ;

} else {

return false ;




Now the functional code becomes:

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

echo $var ; // or do some processing


If your code is all object-oriented, you can put the function in one of the general classes as shown below:

Class GenClass


// class vars


function isNull()




} // end of class

The functional code in this case becomes:

If ( $genClass->isNull($var) ) { // returns true when var has value and $var is not equal to zero

echo $var ; // or do some processing


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