SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot auth1's Avatar
    Join Date
    Nov 2004
    Location
    Melbourne, Australia
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem With NULL

    Hello,
    After reading the comments left at php.net, I relised that many many programmers dislike using the value (if you can call it a value).

    I use the "value" alot in my database, and although, I've read sereval warnings about the excess use, I seem to have no problem.

    What are the issues concerning NULL?

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NULL is just nothing. I set variables equal to NULL all the time. You can accomplish the same thing by setting a variable to 0. The only thing I can think of is that the code might be easier to read for some people if you set a value to 0 instead of NULL.

    Other then that I have no clue.

    bryan

  3. #3
    SitePoint Member
    Join Date
    Nov 2004
    Location
    UK
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm new to php, so don't really know what i'm talking about, but I did read this in a book the other day.

    The author said it's unlikely you often need to use NULL, and you would know if you needed to use it. But he did give this example of when he would use it:

    If you were creating a database that included the age of people, and you wanted to calculate the average age, if someone didn't enter a value in the age form, and it was recorded as '0' (default value) then this would effect the average age of the users, whereas if the value was NULL, it would be ignored, and would have no effect of the average age.

    Just thought it could be of interest....

  4. #4
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can accomplish the same thing by setting a variable to 0.
    Setting a variable to 0 (zero) will set it to zero, not null. It would then fail a data-type comparison.

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  5. #5
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Null in your DB and null in your PHP scripts are really two different issues. You should do a search here and on google with the issues concerning using null values in your DB. Rich R. pointed out one of the issues.

    I use null in my scripts all the time to give something an initial setting.
    Here's an example of a the beginning of a class I'm working on:

    PHP Code:
        
        
    class TrackerLogger {
        
            
    // the constructor
            
    function TrackerLogger($dbType$dbConn=null)
            {
                
    $this->_dbType $dbType;
                
    $this->_dbConn $dbConn;
               
                
    $this->_alreadyLogged    false;
                
    $this->_alreadyConverted false;
        
                
    // set it to null so we'll know if anything 
                // has been assigned to it.
                
    $this->_trackerObj null;
        
                
    // the rest of the constructor down here
        
            
    }
        
            
    //  the rest of the class down here
        
        

    You can pass a DB connection into the constructor if you want, if you don't
    the class will create its own connection.

    So I have some code somewhere in the class like this:

    PHP Code:
        $dbConn $this->_dbConn;
        
        if (
    $dbConn === null)
        {   
            
    $dbConn 
                
    mysql_connect($dbServer$dbUserName$dbPassWord);
        } 
    Then in another method of the same class I might have a line like this:

    PHP Code:
        if ($this->_trackerObj !== null)
        {
            
    $this->_trackerObj->doSomething();
        } 


    --ed

  6. #6
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by launchcode
    Setting a variable to 0 (zero) will set it to zero, not null. It would then fail a data-type comparison.

    Cheers,

    Rich
    Right. 0 is an int. 0 will evaluate to false in a boolean(true or false) expression but it's not the same as null. null is its own type. That's why when I check for null I use === instead of == and !== instead of != . The extra = at the end checks that the type is the same as well as the value.

    You can read about types here:
    http://www.php.net/manual/en/language.types.php

    And comparison operators here:
    http://www.php.net/manual/en/languag...comparison.php

    --ed

  7. #7
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Absolutely, if you don't use === you can get very misleading results back. It's the same with a lot of the PHP functions - strpos for example could legitimately return a zero, but if you don't compare the data type correctly it would evaluate to false, leading to all kinds of mistakes.
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •