Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-07-2011, 07:16 AM   PM User | #1
Nobilis
New to the CF scene

 
Join Date: Oct 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Nobilis is an unknown quantity at this point
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 08:44 AM.. Reason: editing needed
Nobilis is offline   Reply With Quote
Old 10-07-2011, 07:42 AM   PM User | #2
Wanna
Regular Coder

 
Join Date: Sep 2011
Posts: 128
Thanks: 2
Thanked 21 Times in 21 Posts
Wanna is an unknown quantity at this point
You set them as null and you check them as NULL
PHP is case sensitive
Wanna is offline   Reply With Quote
Old 10-07-2011, 07:47 AM   PM User | #3
Nobilis
New to the CF scene

 
Join Date: Oct 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Nobilis is an unknown quantity at this point
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.
Nobilis is offline   Reply With Quote
Old 10-07-2011, 07:58 AM   PM User | #4
Nobilis
New to the CF scene

 
Join Date: Oct 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Nobilis is an unknown quantity at this point
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!
Nobilis is offline   Reply With Quote
Old 10-07-2011, 08:13 AM   PM User | #5
Wanna
Regular Coder

 
Join Date: Sep 2011
Posts: 128
Thanks: 2
Thanked 21 Times in 21 Posts
Wanna is an unknown quantity at this point
You are missing semicolon on the following rule:
PHP Code:
$getPage ' '                //replaced NULL with ' ' 
change it to:
PHP Code:
$getPage ' ';                //replaced NULL with ' ' 
Wanna is offline   Reply With Quote
Old 10-07-2011, 08:21 AM   PM User | #6
Nobilis
New to the CF scene

 
Join Date: Oct 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Nobilis is an unknown quantity at this point
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.
Nobilis is offline   Reply With Quote
Old 10-07-2011, 11:21 AM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
Nobilis (10-08-2011)
Old 10-07-2011, 03:52 PM   PM User | #8
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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.
durangod is offline   Reply With Quote
Old 10-07-2011, 05:44 PM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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'
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-07-2011, 10:55 PM   PM User | #10
Nobilis
New to the CF scene

 
Join Date: Oct 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Nobilis is an unknown quantity at this point
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-07-2011 at 11:08 PM..
Nobilis is offline   Reply With Quote
Old 10-07-2011, 11:15 PM   PM User | #11
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-08-2011, 10:55 AM   PM User | #12
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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.)
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-08-2011, 05:59 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
coding, php, tutorial

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:07 AM.


Advertisement
Log in to turn off these ads.