简体   繁体   中英

nl2br converts true to int and false to empty string

I am using PHP Version 5.6.25. The following code is all that's needed to replicate the issue.

$data['foo'] = true;
$data['bar'] = false;
var_dump($data);
$data['foo'] = nl2br($data['foo']);
$data['bar'] = nl2br($data['bar']);
array_walk_recursive($data, "filter");
var_dump($data);

This gives the following result.

array (size=2)
  'foo' => boolean true
  'bar' => boolean false
array (size=2)
  'foo' => string '1' (length=1)
  'bar' => string '' (length=0)

Is this a PHP bug? Is there a workaround?

from the manual nl2br

string nl2br ( string $string [, bool $is_xhtml = true ] )

nl2br expects a string as an input, casting a boolean to a string returns 1 for true and "" for false; so no surprises that's what you get in this case.

In your filter function you can check the variable type before deciding how to filter it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM