Is it possible to convert null
to string
with php?
For instance,
$string = null;
to
$string = "null";
Is it possible to convert null
to string
with php?
For instance,
$string = null;
to
$string = "null";
Am I missing something here?
if ($string === null) {
$string = 'null';
}
was thinking something shorter...
You can also use a ternary operator:
$string = is_null($string) ? 'null' : $string;
Your call.
if
? –
Talithatalk STRING
for example? regregrege"STRING"ergegergerge
IS NOT what one would expect from this expression. –
Lemuellemuela in PHP 7 you can use Null coalescing operator ??
$string = $string ?? 'null';
starting from 7.4 you can use Null coalescing assignment operator ??=
$string ??= 'null';
Note that it will suppress the error message if $string
doesn't exist. Therefore, it's better to explicitly test the variable with is_null()
:
$string = null;
$string = is_null($string) ? 'null' : $string;
var_dump($string); // string(4) "null"
$string = 'string';
$string = is_null($string) ? 'null' : $string;
var_dump($string); // string(6) "string"
$string = null;
$string = is_null($s) ? 'null' : $string;
var_dump($string); // Warning: Undefined variable $s
Am I missing something here?
if ($string === null) {
$string = 'null';
}
was thinking something shorter...
You can also use a ternary operator:
$string = is_null($string) ? 'null' : $string;
Your call.
if
? –
Talithatalk STRING
for example? regregrege"STRING"ergegergerge
IS NOT what one would expect from this expression. –
Lemuellemuela var_export can represent any variable in parseable string.
In the narrow context where a variable is ONLY null
and the string/word "null" is desired as printed/visible text, there is nothing ugly/unintuitive about using a null coalescing operator, ternary statement, or a comparatively verbose verbose if
. Just be sure to encapsulate your logic with parentheses if you are concatenating the value to another string.
Conversely, using a native function call on an unconditionally null
value:
NULL
or null
,var_dump()
, a newline is appended to the printed value.Here are a battery of solution candidates and their output when fed null
as input: (Demo)
$null = null;
echo 'var_export(): ' . var_export($null, true);
echo "\n";
echo 'var_dump(): ' . (function($v){ ob_start(); var_dump($v); return ob_get_clean(); })($null);
//echo "\n";
echo 'print_r(): ' . print_r($null, true);
echo "\n";
echo 'json_encode(): ' . json_encode($null);
echo "\n";
echo 'serialize(): ' . serialize($null);
echo "\n";
echo 'strval(): ' . strval($null);
echo "\n";
echo 'gettype(): ' . gettype($null);
echo "\n";
echo 'null coalesce: ' . ($null ?? 'null');
echo "\n";
echo 'ternary: ' . ($null === null ? 'null' : $null);
echo "\n";
echo 'condition: ' . (function($v) { if ($v === null) { return 'null'; }})($null);
Output:
var_export(): NULL
var_dump(): NULL
print_r():
json_encode(): null
serialize(): N;
strval():
gettype(): NULL
null coalesce: null
ternary: null
condition: null
As an extension of the scope of the question, using native functions to convert non-string-type data to printable text:
json_encode()
,Ultimately, my advice is to use the null coalescing operator (because it is intuitive/self-documenting, concise, and most performant) unless you have a compelling reason to do otherwise.
it has best solution:
$var = null;
$stringNull = json_encode($var);
you can test it as
$var = null;
$stringNull = json_encode($var);
$null = json_decode($stringNull, true);
var_dump($stringNull);
var_dump($null);
string
to become "string"
with quotes. –
Lemuellemuela © 2022 - 2024 — McMap. All rights reserved.