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.

4 Responses to How to Return Multiple Values from PHP Function

  1. Pingback: Top Five Most Used PHP Array Functions for Beginners

  2. Matias Fito says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">