phpcodacygithub-codereviews

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

  • 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.