0

I wanted to put all the values of session in a variable named after the key so I came up with this code.

foreach($_SESSION as $key => $value){ $$key = $value; } 

For example I want used a session and saved a username on it using

$_SESSION['username'] = "Keith"; 

this means that I can use $username anytime. Now my problem is that how can I use all the information that the above code generates and use it inside a function like:

function get_content(){ echo $username; } 
2
  • I believe this post will solve your problems - since you want to get variables from array keys - but mind that any variables with the same name (in the current scope) will be overwritten. stackoverflow.com/questions/8420103/… Commented Jul 30, 2013 at 4:33
  • 2
    $$key = $_SESSION should be $$key = $value, otherwise each variable will get a copy of the entire session array. Commented Jul 30, 2013 at 4:34

4 Answers 4

4

You have to statefully pass in the variables to the function or use the global keyword.

An easy way to import the variables in an super global array into a functions scope is to use the extract. So in your function you can do this.

function get_contents(){ extract($_SESSION); echo $username; } 
Sign up to request clarification or add additional context in comments.

Comments

2

For this you have to use function parameter

function get_content($var){ return $var; } 

Here you call that function by passing appropriate variable.

echo get_content($username); 

OR You can use extract suggested by Orangepill

function get_contents(){ extract($_SESSION); return $username; } 

Note : Don't use echo inside function to display the result.

2 Comments

What's the difference between get_content($username) and simply $username?
@Barmar OP wants to use that variable inside that function to do any operation on it. In his given code he just want to display it but may be in his actual code he wants some other operation too, so get_content($username) is preferable. :)
2

Okay, so you can use extract() in your function to create local variables, but consider the following as well.

To make your function testable, it might be a better idea to explicitly expect an argument containing the context (an array like $_SESSION, but not necessarily).

function get_content(array $context) { echo $context['username']; } 

To call it you would write:

get_content($_SESSION); 

Note that you can still use extract($context); at the top to create local variables if you don't like the more explicit variable references.

Comments

1

To access a global variable from within a function, you have to use the global declaration:

function get_content(){ global $username; echo $username; } 

Or you can pass the variable as a parameter:

get_content($username); function get_content($username) { echo $username; } 

How you set the variables is irrelevant.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.