Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Why Does ' ' Work Better Than NULL in This Code?

    Hello!

    I am following a tutorial on PHP. The code given below is to get the pages listed in the database to display to the webpage. I already fixed the problem and the code works fine.

    When I used a NULL value for both variables, the $getSubj would work, and display the subject in the content page. $getPage would default to "Select A Subject or Page to Edit...". In the tutorial, of course, it worked.

    I attempted to change the value to '' but that gave me an error. I changed it to " ". That failed as well. Finally, I tried ' ', and this worked. After two days of puzzling, and wonderment, I was able to move on. I changed both the value for $getPage & $getSubj for consistency. PHP doesn't seem to mind.

    My question is why did ' ' finally do the trick, whereas NULL, "" and '' all failed? Why did PHP initially behave the way it did prior to replacing the values? BTW, '' is two single quotes with no space. Thank you in advance for your feedback.


    Here is the function:

    PHP Code:
    function funcFndAll()
    {
      global 
    $getSubj;
      global 
    $getPage;
      
     if(isset(
    $_GET['subj']))
    {
        
    $getSubj funcGetSubID($_GET['subj']);
        
    $getPage ' ';                //replaced NULL with ' '
        
    }
    elseif(isset(
    $_GET['page']))
    {
       
        
    $getSubj ' ';              // replaced NULL with ' '
        
    $getPage funcGetPageID($_GET['page']); 
    }
    else
    {
        
    $getSubj NULL;   
        
    $getPage NULL;
    }

    Here is the code to print that prints
    the subjects and pages to navigation:


    PHP Code:
    <?php
              
    if(($getSubj != null) && ($getPage != null))
             {
                    echo 
    $getSubj['menu_name']; 
                echo 
    $getPage['menu_name'];
             }
          else
             {
                    echo 
    "Select A Subject or Page to Edit...";
             }
    ?>
    <div class="page-content"></div>
        
    <?php echo $getPage['content']; ?>
    Last edited by Nobilis; 10-07-2011 at 09:44 AM. Reason: editing needed

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    You set them as null and you check them as NULL
    PHP is case sensitive

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wanna View Post
    You set them as null and you check them as NULL
    PHP is case sensitive
    Excellent! Thank you. I am going to try making that change in my code and what happens.

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Wanna! Great suggestion but it didn't work. My code still did the same thing. But thank you for bringing that to my attention. I never would have caught that. Much appreciated!

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    You are missing semicolon on the following rule:
    PHP Code:
    $getPage ' '                //replaced NULL with ' ' 
    change it to:
    PHP Code:
    $getPage ' ';                //replaced NULL with ' ' 

  • #6
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wanna View Post
    You are missing semicolon on the following rule:
    PHP Code:
    $getPage ' '                //replaced NULL with ' ' 
    change it to:
    PHP Code:
    $getPage ' ';                //replaced NULL with ' ' 
    You can ignore that. I forgot the semi-colon when I typed in the code. TYVM. I fixed it in the code above.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    I'm not so sure about null being case sensitive.. true and false certainly aren't..

    The difference between null and '' is basically that '' is a string thats been initialised but it is empty. In other words the string has been created but it holds nothing.

    Null means the string, object, whatever doesn't exist. If you tested $Var and it was null it basically means that $Var doesn't actually exist.

    If thats still not quite clear, let me put it like this:
    You can have a plastic container full of sweets. Thats your variable containing something.
    You can have an empty container. Thats your ''
    You can have no container. Thats your null.

    Make sense? - You know that the container could theoretically exist (even if it doesn't) so you can test for it by testing it against null.

    Also you're using ' ' in your code. ' ' is not the same as ''. ' ' contains a space character which although prints nothing on the screen it is a valid ASCII character so its not the same as an empty string.
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    Nobilis (10-08-2011)

  • #8
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,468
    Thanks
    278
    Thanked 32 Times in 31 Posts
    And after the girl takes all your candy and container you also end up with null. LMAO tango thats some funny stuff man. too cool.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by durangod View Post
    And after the girl takes all your candy and container you also end up with null.
    Ah no, thats when you realise you and her were speaking different languages and she actually leaves you with 'nil'
    I can't really think of anything to write here now...

  • #10
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    I'm not so sure about null being case sensitive.. true and false certainly aren't..

    The difference between null and '' is basically that '' is a string thats been initialised but it is empty. In other words the string has been created but it holds nothing.

    Null means the string, object, whatever doesn't exist. If you tested $Var and it was null it basically means that $Var doesn't actually exist.

    If thats still not quite clear, let me put it like this:
    You can have a plastic container full of sweets. Thats your variable containing something.
    You can have an empty container. Thats your ''
    You can have no container. Thats your null.

    Make sense? - You know that the container could theoretically exist (even if it doesn't) so you can test for it by testing it against null.

    Also you're using ' ' in your code. ' ' is not the same as ''. ' ' contains a space character which although prints nothing on the screen it is a valid ASCII character so its not the same as an empty string.
    So with ' ' I am testing for something, whereas NULL is nothing. By swapping out the values I was basically telling PHP check for something...that may come later on...is that right? If I left it at NULL I am telling PHP to test for nothing?
    Last edited by Nobilis; 10-08-2011 at 12:08 AM.

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    You're telling it to test to see if the variable is nothing yes. It's pretty much the opposite of using the isset() function as far as I know.

    There may be better definitions out there but my explanation is the basic principle of it all.
    I can't really think of anything to write here now...

  • #12
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by Wanna View Post
    You set them as null and you check them as NULL
    PHP is case sensitive
    nope. PHP allows you define constants as case-(in)sensitive. NULL is defined case-insensitive (ref.)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The biggest thing to remember is that null represents a void value of \0 regardless of datatype provided; however, null != '' (and also, != ' ' which is a string with a space in it) which differentiates between an initialized string of nothing and a variable of no value. Null is not case sensitive as it is neither a variable or a constant.
    In the above situation, what you would actually have is:
    PHP Code:
    $a null// var dump will provide NULL or \0
    $a ''// var dump will provide string(0), which equates to {''}
    $a 'hello world';
    // var dump will provide string(11), which equates to {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
    // C does not contain a datatype for a string, it is only a char array. 
    Also, an understanding of null is critical in OOP.


  •  

    Tags for this Thread

    Posting Permissions

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