elis Posted April 19, 2009 Share Posted April 19, 2009 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 }?> Link to comment https://forums.phpfreaks.com/topic/154770-solved-need-help-retrieving-a-variable-located-in-a-different-scope/ Share on other sites More sharing options...
DarkSuperHero Posted April 19, 2009 Share Posted April 19, 2009 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? }?> Link to comment https://forums.phpfreaks.com/topic/154770-solved-need-help-retrieving-a-variable-located-in-a-different-scope/#findComment-813874 Share on other sites More sharing options...
elis Posted April 19, 2009 Author Share Posted April 19, 2009 Thank you, using sessions, as you suggested worked perfectly. Link to comment https://forums.phpfreaks.com/topic/154770-solved-need-help-retrieving-a-variable-located-in-a-different-scope/#findComment-813885 Share on other sites More sharing options...
DarkSuperHero Posted April 19, 2009 Share Posted April 19, 2009 great! :-) Link to comment https://forums.phpfreaks.com/topic/154770-solved-need-help-retrieving-a-variable-located-in-a-different-scope/#findComment-813888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.