Jump to content

[SOLVED] Getting array from a function


Northern Flame

Recommended Posts

I have a script that I am working on and I had a problem the

the foreach() command. I was having a hard time figuring out why

it wasnt working and I was messing around with some stuff

and I figured out that I cant use the array that I established in

my function. Take the following sample script as an example:

 

<?php
function testIng(){
$char['test'] = 'test';
$char['testing'] = 'testing';
global $char;
}
testIng();
foreach($char as $key => $val){
echo $key . ' - ' . $val . "<br>\n";
}
?>

 

with that I get this error

 

Warning: Invalid argument supplied for foreach() in /path/to/my/website.com/script.php on line 8

 

can anyone tell my why I cant use the array established in the function testIng()?

Link to comment
https://forums.phpfreaks.com/topic/81886-solved-getting-array-from-a-function/
Share on other sites

<?php
/* 
    Try this! 
    Result 
    test - test
    testing - testing
*/
function testIng(){
$char['test'] = 'test';
$char['testing'] = 'testing';

return $char;
}
$char = testIng();
foreach($char as $key => $val){
echo $key . ' - ' . $val . "<br>\n";
}
?>

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.