views:

41

answers:

2

I have these lines in my view file

////////////////////////////

$a = 5;
showme()

showme()
{
 global $a;
 echo $a;
}

////////////////////////////////

Problem: $a is not accessible in showme() function.

I have no choice to pass $a as argument and no choice to move function from view. and it should be accessible in function through global keyword only...

I can change the way of declaration to $a.

Thanks in advance...

A: 

You are missing the semi-colon to end the later statement:

$a = 5;
showme()

Change to:

$a = 5;
showme();

Your code seems to be ok, it should work, not sure but you may try this if you are inside a class:

$a = 5;
$this->showme();
Sarfraz
A: 

The problem is that $a is actually not defined in the global scope , but within the view template. Therefore,

global $a;

is not working as you expect it.

I am not sure if this will work , but you can at least try it:

$GLOBALS['a'] = 5;

function showme(){
   echo $GLOBALS['a'];
}
Rossen Zahariev