SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What if a variable is null?

    I have my site and the main page is a series of includes... The main content of the page is a include with the filename being a variable pulled from the URL (ie page.php?content=news.php) My problem comes when the client first visits and there is no content variable in the url. How do I check to see if there is a variable there or not so that I can set it.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (!$content) { $content = "news.php"; }

  3. #3
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hrm... That easy? Groovy... Thanx.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are slight differences between testing a variable for its boolean value and checking whether it has been set. There might be circumstances where checking a variables boolean value is not going to give you the correct answer.

    For example:
    PHP Code:
    $milkInYourCoffee false;

    if ( ! 
    $milkinYourCoffee ) {
        
    // The conditions will be satisfied and the if condition will be entered.
       // However it erroneous to conclude that $milkInYourCoffee has not been set
       // however it evaluates to false because that is the value it currently holds
    }

    $lumpsOfSugar 0;

    if (! 
    $lumpsOfSugar ) {
       
    // again it would be erroneous to conclude that $lumpsOfSugar has not been set
       // and again it evaluates to false because 0 evaluates to false

    Quite often when we are validating data - especially when it is user data then testing whether a variable is holding a 0, '' (empty string) or NULL value "(which all evaluate to false) is an appropriate test. Technically, however, it is not the same as using isset()

    From the above code had I called isset() I would have got a different result
    PHP Code:
    $milkInYourCoffee false;
    $result = isset($milkInYourCoffee); // $result will be assigned TRUE

    $lumpsOfSugar 0;
    $result = isset($lumpsOfSugar); // $result will be assigned TRUE 
    To complicate things a little further, one thing that isset() will return false on is if a variable has been set but assigned a value of NULL. If you need to test whether a variable exists but its value is currently NULL then you need to use is_null()

    Clear as mud?

  5. #5
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah... That is what I was looking for. I thought I remembered goiung over that in the Man but could not remember the command. The problem I was running into was that when I tried to do anything with a variable that had no value, the program would create an error and exit the script. I know this is because I have error reporting at a high level, but I'm still building the site... Thanx y'all.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"


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
  •