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';
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.
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.
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() 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 .
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 :
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.
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.
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.
From the documentation:
NULL is always converted to an empty string.
Yes, you can rely on that behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With