Jump to content

[SOLVED] How to access variables declared outside a function from a function?


obay

Recommended Posts

you should try and NOT use something that depends on state if you can. If there were no session because the user had cookies off then you would get NO joy from that.

 

keeping your code as free from state dependance is very good thing.

 

In this case you either pass the variable to teh function or you decalre the variable as global within the function...

 

<?php

$myvar = 'hello';

// passing the variable explicitly...
function fxn ($myvar)
{
echo $myvar;
}

// defining the variables scope from within the func...

function fxn ()
{
global $myvar;
echo $myvar;
}
?>

 

Pros and cons to each so you decide which fits your requirements best.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.