Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, what is the differences between NULL and setting a string to equal 2 single quotes

Tags:

php

null

I used to set things like this when I wanted blank values.

$blankVar = ''; 

Then after some months, I decided this looked better and had a clearer intent.

$blankVar = null; 

This worked without hiccup for a while, but recently with a PDO prepared statements I ran into a problem. Binding a value to null made the query fail, whilst binding it to '' did not. I needed to bind it to null, so that if a condition was met, it would insert blank data.

What are the differences between the 2? I still think equaling null (or at least a constant) looks better, so should I do this?

define('EMPTY', ''); 
like image 836
alex Avatar asked Mar 09 '09 03:03

alex


People also ask

What is the difference between single quotes and double quotes for strings in PHP?

Double-quoted strings: By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences.

What is difference between Null and string?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

What is the difference between null and null in PHP?

Null is case insensitive. From the documentation: There is only one value of type null, and that is the case-insensitive keyword NULL. Unless an exact match in DB is queried.

Does PHP Use single or double quotes?

In PHP, people use single quote to define a constant string, like 'a' , 'my name' , 'abc xyz' , while using double quote to define a string contain identifier like "a $b $c $d" .


2 Answers

Null is just another datatype in PHP, which has only one value (null). Since PHP is a loosly typed language, it can be confusing how it handles different values.

"", 0, "0", False, array(), Null are all considered False in PHP.

Null, however, is a different kind of animal. The main incompatibility with using Null is that you cannot tell if it isset().

$x = false; isset($x)  ->  true echo $x    ->  ""  $y = null; isset($y)  ->  false echo $y    ->  ""  //$z is not set isset($z)  ->  false echo $z    ->  E_NOTICE 

So null is odd in the sense that it doesn't follow normal variable rules in PHP (at least some). In most cases, it is fine.

When it comes to database columns, PHP's NULL has no place there. You see, SQL is a string based language. SQL's NULL must be represented by NULL with no quotes.

So if you want an EMPTY field, set it to ""

INSERT INTO foo SET bar = "" 

But if you want a NULL field, set it to NULL

INSERT INTO foo SET bar = NULL 

BIG DIFFERENCE.

But if you try to insert the PHP NULL directly, it will add zero characters to the query, (which leaves you with a blank or syntax error, depending on if you quoted it).

like image 150
gahooa Avatar answered Sep 23 '22 07:09

gahooa


null is a special placeholder value in the programming language that literally means "nothing". It's not 0, it's not an empty string, it's nothing. There is no value in memory being pointed to. An empty string, on the other hand, is still a string object, just a very short one :)

like image 42
Rex M Avatar answered Sep 24 '22 07:09

Rex M