How to Return Multiple Values from PHP Function

Is it possible to return multiple values from a PHP function. In this article, i’ll try to explain how to return multiple values. Return statement can return only single value at a time, but you can use array or object to return multiple values.

How to Return Multiple Values in PHP

If you want to return multiple values in PHP then make an array and return it.

NOTE – You can’t use multiple return statement inside a function to return multiple values.

Shorter Syntax of Array Declaration in PHP 5.4 .

Let’s understand this concept through example.

1. First Example – Return multiple values in the form of array.

2. Second Example – Returning values in the form of associative array.

Check if value exists in array.

How to Retrieve Multiple Values in PHP

We learned how to pass multiple values in PHP function through arrays. Now question is how to retrieve those values.

1. Using list()

list() is a language construct. list() is used to assign a list of variables in one operation. Through list function(), you can assign array items to variables.

Top five most used PHP functions .

2. Second approach is to retrieve values using their indexes.

Kumar Singh

About Kumar Singh

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.
  • Pingback: Top Five Most Used PHP Array Functions for Beginners()

  • Matias Fito

    Great explanation man, it really helped me!. You should be proud of this arcticle (y)

    • rajkumar9788

      Thanks Matias Fito.

    • http://www.phpist.net/ yassine belkaid

      But there’s no magic in this tuto, very simple, it’s just an array, that’s all about, hope you give better solution, but Thanks anyway