Skip to content
Advertisement

php shorthand return statement

So i was just recently looking for some examples of validating a unix timestamp. One code chunk that kept reappearing was this:

function isValidTimeStamp($strTimestamp) {
    return ((string) (int) $strTimestamp === $strTimestamp) 
        && ($strTimestamp <= PHP_INT_MAX)
        && ($strTimestamp >= ~PHP_INT_MAX);
}

Now I have looked for shorthand return if statements which I think this might be but I am not having any luck. can anyone explain to me how this function is deciding what to return and how. Thanks

Advertisement

Answer

The result of boolean operations (like &&, || or ==) is a boolean, just as the result of numeric operations (like + and *) is a number. So exactly like return 2 + 3 would yield 5, return true && false would return false. Now, operations can of course be nested. For example, return (2 + 3) * (3 + 3) is still a valid expression and yields 30. In the same manner, return ($a === $b) && ($a => $c) will yield a boolean value.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement