Forum Moderators: coopster

Message Too Old, No Replies

How do I access arrays and variables from inside a function?

How do I access variables and arrays inside functions?

         

amnesia440

5:40 am on Jun 1, 2008 (gmt 0)

10+ Year Member



Hello -

This is what I'm trying to do:


$userinfo['name'] = "bob";
$userinfo['lastname'] = "johnson";

function displayinfo() {
echo $userinfo['name'] . " " . $userinfo['lastname'];
}
displayinfo();

Obviously this is a newbie question, but how do I set up my array so it can be accessed in the displayinfo() function?

Thanks
kevin

xiongbin

6:18 am on Jun 1, 2008 (gmt 0)

10+ Year Member



function displayinfo() {
global $userinfo;
echo $userinfo['name'] . " " . $userinfo['lastname'];
}

amnesia440

6:24 am on Jun 1, 2008 (gmt 0)

10+ Year Member



Is this the same as globals in the sense of register_globals (the option that can be turned off at the host level)?

btw - thank you!

dreamcatcher

7:09 am on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi amnesia440,

No, this isn`t the same. This is to do with variable scope. Variables declared outside a function are not the same as variables with the same name inside a function unless you pass the vars globally like in the example.

Superglobals on the other hand, such as $_GET,$_POST,$_COOKIE,$_SESSION are available anywhere in your script right throughout its execution. Same as constants.

This page should help:
[uk2.php.net...]

dc

amnesia440

8:40 pm on Jun 1, 2008 (gmt 0)

10+ Year Member



Thanks dreamcatcher