Krsmoorthi
Krsmoorthi

Reputation: 31

Pass values to function without using parameter in PHP?

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.

Upvotes: 0

Views: 78

Answers (2)

Code Lღver
Code Lღver

Reputation: 15603

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

Upvotes: 0

user254875486
user254875486

Reputation: 11240

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.

Upvotes: 1

Related Questions