Jump to content

return value with function


Orasion

Recommended Posts

Hi all,

 

can you give me suggestion how to return value from function like this

 

function returnArray(){
for($i=1; $i<=3; $i++){
	return $i; //how to return this as array??
}
}

 

when I code "echo" inside the function then call the function it give me output => 123

but when I code "return" inside the function, it will give output => 1

Can you suggest me how to output it as array?

 

Link to comment
https://forums.phpfreaks.com/topic/258304-return-value-with-function/
Share on other sites

In your example $i isn't an array.

 

function returnArray() {
        $foo = array();
for($i=1; $i<=3; $i++){
	return $foo[] = i;
}
        return $foo;
}

 

Will return an array containing the number 1 - 3. Of course you could more easily achieve the same result by using:

 

$foo = range(1,3);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.