Return Values ​​and Exceptions for PHP Functions

I read some books on PHP (notably Welling and Thomson's "PHP and MySQL Web Development"), and I am also a fresh teenager. I was a little curious why the author decided to choose two different ways to stop the function, for example.

if (!$result) {
    throw new Exception('Password could not be changed.');
} else {
    return true;
}

For me, this seems a little inconsistent, and it would be wiser to return false, and the caller checks the returned value of the called subscriber and processes it. Is it common for PHP code? Is this type of style expected when using exceptions?

+3
source share
3 answers

, , . , . true ( ), ( PHP NULL).

+2

, PHP-, , .

, . . , , , .

+1

. , true false , , false .

I don’t think the PHP community has fully developed behavior conventions such as this, so I wouldn’t say that this is either ordinary or unusual. There are hundreds of different ways to create such systems in PHP.

+1
source

Source: https://habr.com/ru/post/1751842/


All Articles