'echo language construct is discouraged. (PHP)

why codacy(code review tool) showing "echo language construct is discouraged."

how to solve this issue.

1: enter image description here



Solution 1:[1]

You can either:

Replace echo with print_r:

print_r( $StateList->state_name )

Return the value instead:

return $StateList->state_name

As for why your linter discourages the use of echo, this depends on your coding standard and context.

For example, if you wish to follow the PSR-1 Basic Coding Standard, section 2.3 states that:

A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both.

Since echo produces a side effect, it would be discouraged in the context of a file that was declaring a new class, for instance. In this context, you'd return the value instead.

Solution 2:[2]

Codacy detects linting problems on your code base. Linting problems may be security bugs, code style issues, etc.

In your case, it's bad practice to use php "echo". Just like you shouldn't use println in other languages. If you need to print something, you should use loggers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 moismailzai
Solution 2 pedrorijo91