How To Return an Array from a Function

Q

How To Return an Array from a Function? - PHP Script Tips - Creating Your Own Functions

✍: FYIcenter.com

A

You can return an array variable like a normal variable using the return statement. No special syntax needed. Here is a PHP script on how to return an array from a function:

<?php
function powerBall() {
  $array = array(rand(1,55), rand(1,55), rand(1,55), 
                 rand(1,55), rand(1,55), rand(1,42));
  return $array;    
}
$numbers = powerBall();
print("Lucky numbers: ".join(",",$numbers)."\n");
?>

This script will print:

Lucky numbers: 35,24,15,7,26,15

If you like those nummers, take them to buy a PowerBall ticket.

2007-04-24, 4695👍, 0💬