php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36019 handling of the string NULL
Submitted: 2006-01-15 07:08 UTC Modified: 2006-01-15 17:40 UTC
From: zola at zolaweb dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.4.2 OS: Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: zola at zolaweb dot com
New email:
PHP Version: OS:

 

 [2006-01-15 07:08 UTC] zola at zolaweb dot com
Description:
------------
Two issues with NULL, one with setting NULL as a string containing the letters NULL, and, when one has successfully set the variable to the word NULL, it getting interpreted differently depending on whether the value is enclosed in single quotes or not.

I do apologize for not being able to check against the latest version, unfortunately it's not possible to get my host to update until they are damn well ready to, but I did not see this issue reported in the bug reports and thus I think it's worth mentioning.

Reproduce code:
---------------
I had a form that had several values that didn't have to be set. I wanted to use the word "NULL" as a word (as opposed to the NULL constant) to go into the SQL statement when creating a new record.

If I do a check based on the variable having no value:

if ($array['var'] == "")
{
$array['var'] = "NULL";
}

Then $array['var'] contains the word "NULL" the way I want it to, BUT I have to be careful with the sql statement (more on this in a moment)

On the other hand, if I check via !isset()

if (!isset($array['var']))
{
$array['var'] = "NULL";
}

It treats NULL as the constant and unsets the variable.

In the SQL, if I am inserting and put it in as is:

$sql = "INSERT INTO mytable VALUES(NULL, ".$array['var'] .", ' ".$array['some_other_var'] ." ',)";

the word NULL replaces $array['var'] as it should, but if I enclose the variable in single quotes (because maybe that variable, if it's set, will contain a space)

$sql = "INSERT INTO mytable VALUES(NULL, ' ".$array['var'] ." ', ' ".$array['some_other_var'] ." ',)";

again, it treats NULL as the constant NULL instead of the word.

Expected result:
----------------
I would expect that if I am using the string NULL, it will not be treated as the constant NULL, or at the least for the behavior to be consistent--that it will ALWAYS treat it as the constant NULL instead of treating it one way when it isn't enclosed and another way when it is.

Actual result:
--------------
in isset:

$array['var'] is not set to anything

in the other example

$array['var'] contains the word NULL

The first sql line resolves to

INSERT INTO mytable VALUES(NULL, NULL, 'some_value')";

The second resolves to

INSERT INTO mytable VALUES(NULL, '', 'some_value')";

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-15 13:24 UTC] tony2001@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.



 [2006-01-15 13:45 UTC] tony2001@php.net
Nevermind, your assumption that "NULL" should be treated as NULL is wrong.
"NULL" is just a string, while NULL is a special value.
No bug here.
 [2006-01-15 17:21 UTC] zola at zolaweb dot com
Actually, that's not it. It SHOULD be treated as a string, but is treated as NULL sometimes, but as a string other times.

if you enclose in single quotes or apply any function to it, it's treated as the construct NULL. If you don't enclose, it's treated as a string.

The change in behavior in the same circumstance is what I'm calling to your attention.

$array['var'] = "NULL";

In this example, it will yield NULL the string.

$sql = "INSERT INTO mytable ($ID, ".$array[['var']).",".$array[['othervar']).")";


If you echo the sql statement, you see this:

INSERT INTO mytable (ID, NULL, othervar)


If you enclose the variable containing the string NULL in single quotes, you get NULL acting as NULL not a string at all:

$sql = "INSERT INTO mytable ($ID, '".$array[['var'])."',".$array[['othervar']).")";

INSERT INTO mytable (ID, '', othervar)

Putting the variable in a function also makes it act as NULL:

$sql = "INSERT INTO mytable ($ID, ".strtoupper($array[['var'])).",".$array[['othervar']).")";


also yields

INSERT INTO mytable (ID, '', othervar)
 [2006-01-15 17:40 UTC] tony2001@php.net
>It SHOULD be treated as a string
No.

>but is treated as NULL sometimes, but as a string other times.
No, you're wrong again.
No bug here.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 18:01:33 2025 UTC