Jump to content

[SOLVED] Need help retrieving a variable located in a different scope


elis

Recommended Posts

I'm creating an install script, and in one part of the script, the user inputs a path and filename, which creates and writes to a file.

 

My problem is that I need to open and read this file later on the script, but because the variables to the name the user set for the file is declared in a different scope, I can't access the name; nor can I change the scope it's declared in (the program won't work correctly if I do)

 

Does anyone know how I can retrieve the variables that are located in a different scope?

 

Below is a VERY compact version of the script, not including the file creating mentioned earlier, just to illustrate where I'm having issues

<?PHP if(isset($_POST['1'])) {
$filename = $_POST['filename']; //the variable I need to retrieve
$path = $_POST['path'];

}
elseif($_GET['cont'] == 2) {
echo $filename; //can't retrieve variable contents in this scope
}?>

 

if this is used during an installation process, why not use cookies or sessions to store some of these variables temporarily...

 

but your problem might be fixed by setting your variable outside the conditions...

 

<?PHP 
$filename = $_POST['filename']; //the variable you need to retrieve
if(isset($_POST['1'])) {
$filename = $_POST['filename']; //override it with a new value if $_POST['1'] is set?
$path = $_POST['path'];

}
elseif($_GET['cont'] == 2) {
echo $filename; //retrieve old value i suppose?
}?>

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.