为什么 base64_encode 会失败?

Why should base64_encode fail?

Description

Encodes the given data with base64.

This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.

Base64-encoded data takes about 33% more space than the original data.

Return Values

The encoded data, as a string or FALSE on failure.

上述 quote 摘自 PHP.net 声称该功能可能会失败。但是出于什么原因?

它说失败时可能 return false。一个例子是,如果你尝试像这样编码一个数组:

if(base64_encode([])){
  echo "Encoded";
} else {
  echo "Not encoded"; //It will execute this
}

简单,

如果您尝试编码的参数由于不是有效数据类型而无法编码,base64_encode() 将失败。我猜它会 return false 如果是的话。

例如:

$array = array('bla');
var_dump($array);

会失败并且 return 错误。它还会抛出一个 E_WARNING 用于传递无效参数。