0

This is probably not the wisest question, but is it possible to return a variable from a function without redeclaring it?

function test(){
$a = 1;
return $a;
}

Would it somehow be possible to access $a with just calling the function? test();

not $a = test();

Is that the only way? I want to make $a available after calling the function.

5
  • what is your reason? just a whim? Commented Nov 6, 2011 at 15:26
  • Just interested in knowing. I have about 30 pages which call up a function that returns a variable that those pages use. I figured I could save some time. Commented Nov 6, 2011 at 15:34
  • save time on what? on typing $var=? OMG. You had vasted already ten times more asking this question. And made your code worse. Commented Nov 6, 2011 at 15:44
  • not every knowledge worth gaining Commented Nov 6, 2011 at 15:51
  • Col Shrapnel addresses a very valid point, I added an answer that tries to show the bigger picture. You're not saving time actually, you're starting to make your code cost more time to you if you plan to change it in the future (which I guess you want as you're starting). Commented Nov 6, 2011 at 15:51

4 Answers 4

1

You have to put the variable into the global scope:

http://codepad.org/PzKxWGbU

function test()
{
  $GLOBALS['a'] = 'Hello World!';
}
test();
echo $a;

But it would be better to use a return value or references:

function test(&$ref)
{
  $ref = 'Hello World!';
}
$var = '';
test($var);
Sign up to request clarification or add additional context in comments.

Comments

1

I assume you want to change some context when you call the function.

This is possible. For example, all global variables share the same context across your application. If a function set's a global variable, its available in the global scope:

function change_global_variable($name, $value)
{
    $GLOBALS[$name] = $value;
}

change_global_variable('a', 1);
echo $a;

However, whenever you do something like this, take care that you're destroying the modularity of your code. For example, if you have 50 functions like this and 80 variables, and then you need to change something, you need to change many, many places. And even harder, you don't remember which places you need to change and how they belong to each other.

It's normally better to make the context an object and pass it to the function that changes it, so it's more clear what the function does:

class Context extends ArrayObject {}

$context = new Context;

function change_context(Context $context, $name, $value)
{
    $context[$name] = $value;
}

change_context($context, 'b', 2);

echo $context['b'], "\n";

This example demonstrates something that's called dependency injection. Instead of modifying the only one (global) context that exists in PHP, you are telling the function what it should modify, instead that it modifies hard-encoded global variables

There is a mix form of it, using the global variable scope to contain the context, so you spare to pass it as an additional parameter, however I only show it here for completeness, because it's not helping to retain modularity that well (however it's better than using more than one global variable):

class Context extends ArrayObject {}

$context = new Context;

function change_global_context($name, $value)
{
    global $context;
    $context[$name] = $value;
}

change_global_context('c', 3);

echo $context['c'], "\n";

Rule of thumb: Avoid global variables. Consider them of being very expensive and you don't want your code to be expensive.

Comments

0

No, it is not due to the scoping.

An option would be to declare $a before and then making it a global. It will then be available inside test:

$a = '';

function test(){
    global $a;
    $a = 1;
    return $a;
}

echo $a; // 1

But may I ask why you want to do this? Are you just playing around or do you have a use case? You're most probably doing it wrong if you need this.

1 Comment

I'm recoding a script. Most of the pages use a variable that was defined globally before. I made a session checking function that returns the same variable in case the user is logged on. I thought I'd save some time.
-1

Maybe if you just want to show the variable you can use

function test(){
  $a = 1;
  echo $a;
  return $a;
}

in this way if you want you can save the variable $a in a variable if not you can just show it calling the test function.

Hope it helps.

Nicola.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.