0

How can we pass the variable value to function without using any parameter? Once we run the script variable value can be echo within the function.

2
  • 4
    Why would you want to do that? What's wrong with parameters? Commented Dec 7, 2011 at 6:10
  • The only solution would be to use a global variable, but I don't really see the need for that. Maybe if you expand your question it would be easier to understand what you are trying to do Commented Dec 13, 2011 at 11:04

2 Answers 2

1

You could use the keyword global, although I think you shouldn't. Example:

$a = 'foo';
bar();

function bar(){
    global $a;
    echo $a;
}

Above code will print "foo";

Again, I really think you should not use this and come up with some other implementation that doesn't require the use of global variables.

Sign up to request clarification or add additional context in comments.

Comments

0

You can access the value of variable by declaring the variable as global variable.

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.