开发者

php make it say null

An example of where I have a variable and its value is "null".However, when I echo it out, it shows as nothing (because its null).

test.php:

<?php

$a=null;
echo 'start'.$a.'end';

?>

Is there a way to make this output 'startnullend' when the file test.php is requested in firefox? var_dump does not work, nor does print_r for some reason开发者_如何学C. The easy way is to IF test for null and then output null using if statement, but is there a different way?


You can use var_dump or print_r to show the value.


I don't know why this couldn't in the other question, but use this

<?php
   $a=null;
   echo 'a'.($a===null?'null':$a).'b';
?>


Casting null to a string results in an empty string. If you want to output null, you will have to explicitly do:

echo 'start'.((is_null($a)) ? 'null' : '').'end';

Or something else similar.


Try this

echo 'start'.(isset($a) ? $a : 'null').'end';


Do a var_dump instead of an echo

var_dump($var)

Shows the type as well as the value


you can use var_dump or print_r


There is a function for that - var_dump();

or, try this function:

function getReadableVar($var){
if($var === null){
 $var = "null";
}elseif($var === true){
 $var = "true";
}elseif($var === false){
 $var = "false";
}
return $var;
}

echo getReadableVar($var);
//or
echo var_dump($var);


<?php
  // prints 'null', if $var is null
  echo is_null($var) ? 'null' : $var;
  // prints 'true' or 'false'
  echo (bool)$var ? 'true' : 'false'
?>


Cast it to a string first:

echo (string)$myvar;


Make it a string?

$a = 'null';


Write a function to do it and invoke it when you want to check for null.

function n($var) {
    return $var === null ? "null" : $var;
}

And use it each time.

$a=null;
echo 'start'.n($a).'end';


If you want to show up on screen the string NULL then :

$a = "NULL";

otherwise null is a reserved word yeah?


function toString($a) {
 return is_null($a) ? "null" : $a;
}

(not tested)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜