Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a function have multiple return value types in PHP?

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?

like image 595
Sunny Avatar asked Nov 28 '11 19:11

Sunny


People also ask

Can a function have multiple return types?

You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values. There is no specific syntax for returning multiple values, but these methods act as a good substitute.

Can a method have 2 returns?

No, you can not have two returns in a function, the first return will exit the function you will need to create an object.


1 Answers

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

like image 102
NikiC Avatar answered Sep 26 '22 00:09

NikiC