Home  >  Article  >  Backend Development  >  Is there only one return statement allowed in a function in PHP?

Is there only one return statement allowed in a function in PHP?

烟雨青岚
烟雨青岚Original
2020-07-08 14:21:447174browse

Not only one return statement is allowed in a function in PHP. In a PHP function, only one return statement can be executed; but multiple return statements can be written; because if the function executes the return statement, the life of the function is over, and the code following the return statement will not be executed.

Is there only one return statement allowed in a function in PHP?

Not only one return statement is allowed in a function in PHP.

If the function executes the return statement, then the life of the function is over, and the code after the return statement will not be executed. So to be precise, a return statement can only be executed once in a function, but multiple return statements can be written.

For example:

def test_return(x):
    if x > 0:
        return x
    else:
        return 0

Recommended tutorial: "php tutorial"

The above is the detailed content of Is there only one return statement allowed in a function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn