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?
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.
No, you can not have two returns in a function, the first return will exit the function you will need to create an object.
Yes. PHP is loosely typed, so any variable can take any type. Function return values are no exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With