Skip to content

How to Create Function with Multiple Returns in PHP

If you need to create a PHP function that returns multiple values, then you can do one of the following.

Option 1 – Returning an array

function returnArray() { return array("one", "two"); } // call the function and print the response var_dump(returnArray());
Code language: PHP (php)

Option 2 – Returning a conditional

function returnConditional($x = true) { if ($x) return "one"; else return "two"; } // call the function with a value and print the response var_dump(returnConditional(false));
Code language: PHP (php)

Option 3 – Using generator to yield values

function multipleValues() { yield "one"; yield "two"; } $res = multipleValues(); foreach($res as $r) { echo $r; // first val=="one", second val=="two" }
Code language: PHP (php)

See also  Actionscript 2 - PHP Data Transfer
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x