Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert null to string

Tags:

php

null

isnull

Is it possible to convert null to string with php?

For instance,

$string = null;

to

$string = "null";
like image 711
Run Avatar asked Mar 28 '12 18:03

Run


2 Answers

Am I missing something here?

if ($string === null) {
    $string = 'null';
}

was thinking something shorter...

You can do it in one line, and omit the braces:

if ($string === null) $string = 'null';

You can also use the conditional operator:

$string = ($string === null) ? 'null' : $string;

Your call.

like image 114
Matt Ball Avatar answered Sep 20 '22 13:09

Matt Ball


var_export can represent any variable in parseable string.

like image 26
dev-null-dweller Avatar answered Sep 20 '22 13:09

dev-null-dweller