Error Identifier: echo.nonString
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
function doFoo(): void
{
echo [];
}
Why is it reported? #
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.
How to fix it #
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([]);
}
How to ignore this error #
You can use the identifier echo.nonString to ignore this error using a comment:
// @phpstan-ignore echo.nonString
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: echo.nonString
Rules that report this error #
- PHPStan\Rules\Cast\EchoRule [1]