website/errors/possiblyImpure.echo.md
<?php declare(strict_types = 1);
/** @phpstan-pure */
function formatAndPrint(string $name): string
{
$result = 'Hello, ' . $name;
echo $result;
return $result;
}
This identifier is not reported in practice because echo is always considered definitely impure, not just possibly impure. PHPStan reports impure.echo instead.
The function or method is marked as @phpstan-pure, but it contains an echo statement. Pure functions must not have side effects -- they should only compute and return a value based on their input parameters. Outputting text with echo is a side effect because it modifies the program's output stream.
Remove the echo statement from the pure function and let the caller handle the output:
<?php declare(strict_types = 1);
/** @phpstan-pure */
function formatAndPrint(string $name): string
{
$result = 'Hello, ' . $name;
- echo $result;
return $result;
}
+
+echo formatAndPrint('World');
Alternatively, if the function needs to produce output, remove the @phpstan-pure annotation:
<?php declare(strict_types = 1);
-/** @phpstan-pure */
function formatAndPrint(string $name): string
{
$result = 'Hello, ' . $name;
echo $result;
return $result;
}