MySQL
  Home arrow MySQL arrow Null and Empty Strings
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MYSQL

Null and Empty Strings
By: Shikhar Kumar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2008-12-02


    Table of Contents:
  • Null and Empty Strings
  • Making Null Safe in PHP
  • Null and MySQL
  • Handling Null in MySQL

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Null and Empty Strings
    ( Page 1 of 4 )

    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.

    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.


    <?php

    $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


    OR


    $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
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek