By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Question about Null values

P: n/a
A section of the documentation for the isSet() function states:

Also note that a NULL byte ("\0") is not equivalent to the PHP NULL
constant.

Where would you encounter a NULL byte? Is a null byte what is returned
from a field in a database?

Josh
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Joshua Ruppert:
A section of the documentation for the isSet() function states:

Also note that a NULL byte ("\0") is not equivalent to the PHP NULL
constant.

Where would you encounter a NULL byte? Is a null byte what is returned
from a field in a database?


It's typically used in C to mark the end of a string. As you might know the
programmer is responsible for managing memory in C. This means that if you
want to store a string coming from the user using C, you will have to set
aside some buffer space, say 4096B. If the user sends a string of only 67B
the \0 byte is used in this buffer to mark the end of the string.

Luckily it's not anything you really have to worry about in PHP.

André Nęss
Jul 17 '05 #2

P: n/a
> It's typically used in C to mark the end of a string. As you might know the
programmer is responsible for managing memory in C. This means that if you
want to store a string coming from the user using C, you will have to set
aside some buffer space, say 4096B. If the user sends a string of only 67B
the \0 byte is used in this buffer to mark the end of the string.

Luckily it's not anything you really have to worry about in PHP.


right - in php, the NULL Character is a Character like every other (besindes some functions don't work right with it)

But nothing should give you such a Character (besindes the evil evil user)
--
mfg Christian (Chronial "at" web.de)

--
Composed with Newz Crawler 1.5 http://www.newzcrawler.com/
Jul 17 '05 #3

P: n/a
On Tue, 16 Dec 2003 12:03:31 +0100, "Christian Fersch"
<Fr********@web.de> wrote:
It's typically used in C to mark the end of a string. As you might know the
programmer is responsible for managing memory in C. This means that if you
want to store a string coming from the user using C, you will have to set
aside some buffer space, say 4096B. If the user sends a string of only 67B
the \0 byte is used in this buffer to mark the end of the string.

Luckily it's not anything you really have to worry about in PHP.


right - in php, the NULL Character is a Character like every other (besindes some functions don't work right with it)

But nothing should give you such a Character (besindes the evil evil user)


If you were to retrieve a BLOB field from a database, it could contain
"null" (\0) characters.

In database terms, "NULL" means "no data". The "null character" *is*
data - a byte with a value of zero.

--
David ( @priz.co.uk )
The Internet Prisoner Database: http://www.priz.co.uk/ipdb/
The Tarbrax Chronicle: http://www.tarbraxchronicle.com/
Jul 17 '05 #4

P: n/a
On Tue, 16 Dec 2003 11:18:55 +0000, David Mackenzie hath writ:
....... The "null character" *is* data - a byte with a value of zero.


More properly: "...a byte with a value of" *nothing* .
Not zero , not alpha , not numeric , not feet , not inches...
At best it is a placeholder -- where _something_ could be -- but,
at the present time, there is *nothing* there.

As a thing meaning *nothing*, in C-like constructs it is usually
used the show the end of *something* .

Jonesy
--
| Marvin L Jones | jonz | W3DHJ | OS/2
| Gunnison, Colorado | @ | Jonesy | linux __
| 7,703' -- 2,345m | config.com | DM68mn SK
Jul 17 '05 #5

P: n/a
On 16 Dec 2003 16:12:12 GMT, Allodoxaphobia <bi********@config.com> wrote:
On Tue, 16 Dec 2003 11:18:55 +0000, David Mackenzie hath writ:
....... The "null character" *is* data - a byte with a value of zero.
More properly: "...a byte with a value of" *nothing* .
Not zero , not alpha , not numeric , not feet , not inches...
At best it is a placeholder -- where _something_ could be -- but,
at the present time, there is *nothing* there.


Well, a NUL character is CHR(0) and is a one-byte zero, it is very much
something. You seem to be almost describing an SQL database NULL, which
represents 'unknown'.
As a thing meaning *nothing*, in C-like constructs it is usually
used the show the end of *something* .


Character strings are terminated in C by a NUL (chr(0)) character. It is
definitely 'something' itself, since you have to allocate space for it. PHP's a
higher-level language so you don't have to worry about NUL terminators. And you
can have NUL bytes in PHP 'string' variables, as they're actually
'binary-safe'.

<pre>
<?php
$x = chr(0).chr(0).chr(0).chr(0).'x';
var_dump($x);
?>
</pre>

string(5) "x"

(Note the length!)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #6

P: n/a
So would a null byte be basically an empty string as opposed to an
unknown value?

Josh

Andy Hassall wrote:
On 16 Dec 2003 16:12:12 GMT, Allodoxaphobia <bi********@config.com> wrote:

On Tue, 16 Dec 2003 11:18:55 +0000, David Mackenzie hath writ:

....... The "null character" *is* data - a byte with a value of zero.


More properly: "...a byte with a value of" *nothing* .
Not zero , not alpha , not numeric , not feet , not inches...
At best it is a placeholder -- where _something_ could be -- but,
at the present time, there is *nothing* there.

Well, a NUL character is CHR(0) and is a one-byte zero, it is very much
something. You seem to be almost describing an SQL database NULL, which
represents 'unknown'.

As a thing meaning *nothing*, in C-like constructs it is usually
used the show the end of *something* .

Character strings are terminated in C by a NUL (chr(0)) character. It is
definitely 'something' itself, since you have to allocate space for it. PHP's a
higher-level language so you don't have to worry about NUL terminators. And you
can have NUL bytes in PHP 'string' variables, as they're actually
'binary-safe'.

<pre>
<?php
$x = chr(0).chr(0).chr(0).chr(0).'x';
var_dump($x);
?>
</pre>

string(5) "x"

(Note the length!)

Jul 17 '05 #7

P: n/a
On Tue, 16 Dec 2003 23:04:36 GMT, Joshua Ruppert
<no************@rochester.rr.com> wrote:
So would a null byte be basically an empty string as opposed to an
unknown value?


In which language?

C: Yes, a single NUL character is the representation of a zero-length string.
PHP: No, it'd be a 1-character string consisting of a chr(0).

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #8

P: n/a
Another Newbie here...

doing a small order tracking app to learn PHP, MySQL, etc...

With regard to null values, I have a customer entry form in html.Some
fields are optional. I send data to MySQL via a POST method. When I
try to query the database later (via a form as well) and use "IS
NULL", for example:

select c.lname, c.fname
from Customer c
where c.company_name is null

....I get no results. I have to query the db with something like

select c.lname, c.fname
from Customer c
where (c.company_name='')

What am I doing wrong? There do not seem to be any null values in my
db, only empty values ('').

I suspect it has to do with the addslashes() function applied to the
post variables on the customer insert page. Must I NOT addslashes() if
the POST field has been skipped by the user?

Thanks!

On Tue, 16 Dec 2003 23:25:55 +0000, Andy Hassall <an**@andyh.co.uk>
wrote:
On Tue, 16 Dec 2003 23:04:36 GMT, Joshua Ruppert
<no************@rochester.rr.com> wrote:
So would a null byte be basically an empty string as opposed to an
unknown value?


In which language?

C: Yes, a single NUL character is the representation of a zero-length string.
PHP: No, it'd be a 1-character string consisting of a chr(0).


Jul 17 '05 #9

P: n/a
Adam M. Johnson <ad***@althealth.org> wrote:
With regard to null values, I have a customer entry form in html.Some
fields are optional. I send data to MySQL via a POST method. When I
try to query the database later (via a form as well) and use "IS
NULL", for example:

select c.lname, c.fname
from Customer c
where c.company_name is null

...I get no results. I have to query the db with something like

select c.lname, c.fname
from Customer c
where (c.company_name='')

What am I doing wrong? There do not seem to be any null values in my
db, only empty values ('').

I suspect it has to do with the addslashes() function applied to the
post variables on the customer insert page. Must I NOT addslashes() if
the POST field has been skipped by the user?


perhaps you can test your POST variables before inserting them in the
database, i.e.

if (empty($_POST['myvar'])) {
$myvar = 'NULL';
}
else {
// note the surrounding quotes
$myvar = "'".mysql_escape_string(stripslashes($_POST['myvar']))."'";
}

mysql_query("UPDATE table SET field=".$myvar);
(or INSERT, whatever you are using...)

if they are empty, NULL will be inserted in the database; if not,
stripslashes strips \ from the variable the form transmits, and
mysql_escape_string escapes special characters when passing the sql query
statement.
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.