Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to concatenate `null` with a string in PHP?

I didn't found a statement about the compatibility between strings and null in the docs, but trying this on PHP 5.5:

echo 'foo' . null . 'bar'; 

prints out foobar.

I wonder if this behaviour is guaranteed, or "safe" to do (in SQL, for example, it is not)? Or asking the other way: Would I ever need to check for null before concatenating strings? Like

echo 'foo' . (($mystring === null) ? '' : $mystring) . 'bar'; 
like image 891
Foo Bar Avatar asked Aug 29 '15 09:08

Foo Bar


People also ask

Can you concatenate a string with null?

Using the String. The String. concat() method is a good choice when we want to concatenate String objects. The empty String returned by the getNonNullString() method gets concatenated to the result, thus ignoring the null objects.

Can we concatenate null values?

Concatenating Data When There Are NULL ValuesTo resolve the NULL values in string concatenation, we can use the ISNULL() function. In the below query, the ISNULL() function checks an individual column and if it is NULL, it replaces it with a space.

What happens when you concatenate null?

When SET CONCAT_NULL_YIELDS_NULL is ON, concatenating a null value with a string yields a NULL result. For example, SELECT 'abc' + NULL yields NULL .

Is null and empty string the same in PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

How to concatenate two strings in PHP?

Concatenation of two strings in PHP. There are two string operators. The first is the concatenation operator (‘. ‘), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘ .= ‘), which appends the argument on the right side to the argument on the left side. Examples :

How to concatenate null to string in JavaScript?

To concatenate null to a string, use the + operator. Let’s say the following is our string. We will now see how to effortlessly concatenate null to string. The following is the final example.

What are the string operators in PHP?

There are two string operators provided by PHP. 1.Concatenation Operator ("."): This operator combines two string values and returns it as a new string. 2.Concatenating Assignment operator (".="): This operation attaches the argument on the right side to the argument on the left side.

What is the use of concatenation and assignment operators in JavaScript?

1.Concatenation Operator ("."): This operator combines two string values and returns it as a new string. 2.Concatenating Assignment operator (".="): This operation attaches the argument on the right side to the argument on the left side. Let's demonstrate the utility of the above operators by following examples.


1 Answers

From the documentation:

NULL is always converted to an empty string.


Yes, you can rely on that behavior.

like image 191
user3942918 Avatar answered Sep 18 '22 19:09

user3942918