Can you return multiple values in php?
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article PHP doesn’t support to return multiple values in a function. Inside a function when the first return statement is executed, it will direct control
back to the calling function and second return statement will never get executed. However, there are ways to work around this limitation. php
Output: Factors of 24 are: 1 2 3 4 6 8 12 Example 2: This example uses list function to store the swapped value of variable. It is used to assign array values to multiple variables at the same time. The multiple values
returned in array from the function can be assigned to corresponding variables using list(). php
Output: Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 10 PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Technically, you can't return more than one value. However, there are multiple ways to work around that limitation. The way that acts most like returning multiple values, is with the
Technically, you're returning an array and using The
This technique is also used in some functions defined by php itself (e.g. A third method is to use an object to hold the different values you need. This is more typing, so it's not used quite as often as the two methods above. It may make sense to use this, though, when using the same set of variables in a number of places (or of course, working in a language that doesn't support the above methods or allows you to do this without extra typing).
The above methods sum up the main ways of returning multiple values from a function. However, there are variations on these methods. The most interesting variations to look at, are those in which you are actually returning an array, simply because there's so much you can do with arrays in PHP. First, we can simply return an array and not treat it as anything but an array:
The most interesting part about the code above is that the code inside the function is the same as in the very first example I provided; only the code calling the function changed. This means that it's up to the one calling the function how to treat the result the function returns. Alternatively, one could use an associative array:
Php does have the
It should be noted that while Finally, I would like to mention that
However, the following will do something different:
If you used How can I return multiple data from a function in PHP?PHP doesn't support to return multiple values in a function. Inside a function when the first return statement is executed, it will direct control back to the calling function and second return statement will never get executed.
Can return return multiple values?JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.
How do I return multiple values from a return statement?We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data.
How pass multiple values in array in PHP?Create an instance for your view file and use predefined method of a controller file to set variables. Like this way: return new ViewModel(array( 'order_by' => $order_by, 'order' => $order, 'page' => $page, 'paginator' => $paginator, ));
|