0

I know in python we can do

Def foo():
   return "foo", "bar", 10

a, b, c = foo()

In php I have to do

public function bar(){
   return ["foo", "bar"]
}

$arr = bar()
$a = $arr[0]
$b = $arr[1]

Is there anyway I can assign multiple variables from function returns in php?

Robert Tirta
  • 2,593
  • 3
  • 18
  • 37

1 Answers1

0

You can return multiple values with Python but you can't with PHP, however, you can return multiple value in form of an array and use list() to assign value to variables as if they were in an array

function bar(){
   return ["foo", "bar"];
}

list($a, $b) = bar();
echo $a; // show "foo"
echo $b; // show "bar"
catcon
  • 1,295
  • 1
  • 9
  • 18