Have you ever declared a function and wished you could get access to the variables within that function? As you may know, this is unable to happen until you make one simple declaration in that function.

function somefunction() {

     //DECLARE THIS AND MAKE YOUR VARIABLE RECOGNIZED OUTSIDE THE FUNCTION
     global $var1, $var2, $var3;

     $var1 = “This text can now be seen outside of the function”;
     $var2 = 2; //So can this variable
     $var3 = 30+$var2; //So can this variable!
}

Now the variables that are used in the function can be called out of it, such as

somefunction();

echo $var1;
echo $var2;
echo $var3;

I hope this helps some of you out there! Just remember global and you cannot do this(global $var2 = 2;) Just declare global, then set values in the variables.

No comments yet.

Would you like to login?