website/errors/echo.nonString.md
<?php declare(strict_types = 1);
function doFoo(): void
{
echo [];
}
The echo language construct expects values that can be converted to a string. Passing a value that cannot be converted to a string -- such as an array or an object without a __toString() method -- will cause a TypeError at runtime. In the example above, an array is passed to echo, which PHP cannot convert to a string.
Convert the value to a string before passing it to echo:
<?php declare(strict_types = 1);
function doFoo(): void
{
- echo [];
+ echo implode(', ', []);
}
Or use a function designed for the value type:
<?php declare(strict_types = 1);
function doFoo(): void
{
- echo [];
+ print_r([]);
}