Reputation: 566
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
Upvotes: 1
Views: 1598
Reputation: 15459
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.
Upvotes: 3
Reputation: 7534
this is not unique to PHP (every language I know allows this). all that is happening here is that a condition (in this case a series of 3 conditions) is being evaluated and the result of that evaluation is being returned.
This function will either return true
or false
depending on all the conditions being met or not.
Upvotes: 1