Alex Rivera | Logout

Can a function have multiple return value types in PHP?

Asked 2011-11-28T19:26:43.767
8

Is there a way I can have my PHP function return a different value type?

The following code should explain what I mean:

<?php
    public function test($value){
        if($value == 1){
            return "SUCCESS";
        } else {
            return false;
        }
    }
?>

On one condition I'm returning a string, otherwise I'm returning a Boolean. Is that allowed/possible?

Edit
Report

1 Answer

14

Yes. PHP is loosely typed, so any variable can take any type. Function return values are no exception.

answered 2011-11-28T19:27:51.563

Your Answer