scottieclark Posted September 12, 2010 Share Posted September 12, 2010 Hi. I'm sure this is a really simple problem!..but my inexperience is really showing me up! The problem is with defining the file that the function should read into $lines array. $lines = file('http://www.nickalm.com/rebuild/scripts/oil.txt'); With a string as the definition it works fine but with a variable (which I need) - no joy! Here's some code that works just fine... <?php function get_value_of($name){ $lines = file('http://www.nickalm.com/rebuild/scripts/oil.txt'); // this is the troublesome little devil here foreach (array_values($lines) as $line) { list($key, $val) = explode('=', trim($line) ); if (trim($key) == $name) { return $val; iconv( "UTF-8", "ISO-1252//TRANSLIT", $val ); } } return false; } ?> ...however...this just doesn't: $title_path = 'http://www.nickalm.com/rebuild/scripts/'.$gallery.'.txt'; function get_value_of($name) { $lines = file($GLOBALS['$title_path']); // this is the troublesome little devil here foreach (array_values($lines) as $line) { list($key, $val) = explode('=', trim($line) ); if (trim($key) == $name) { return $val; iconv( "UTF-8", "ISO-1252//TRANSLIT", $val ); } } return false; } I've tried it every which way I can think of, firstly using just $title_path, then $GLOBALS['$title_path'] when I thought it was a scope issue, but no luck so far! Any tips or pointers MUCH appreciated! Thanks. Scott Quote Link to comment https://forums.phpfreaks.com/topic/213208-var-scopesimple-problemgot-me-beat/ Share on other sites More sharing options...
MadTechie Posted September 12, 2010 Share Posted September 12, 2010 you could us global ie $filePath = "oil.ini"; echo get_value_of('five'); function get_value_of($name){ global $filePath; $ini_array = parse_ini_file($filePath); return $ini_array[$name]; } or a class class readINI{ private $ini_array; public function __construct($filePath) { $this->ini_array = parse_ini_file($filePath); } function get_value_of($name){ return $this->ini_array[$name]; } } //use class $oilINI = new readINI('oil.ini'); echo $oilINI->get_value_of('five'); EDIT: I should of said, with the class you can get multiple results like this $oilINI = new readINI('oil.ini'); //loaded echo $oilINI->get_value_of('one'); //display 1 echo $oilINI->get_value_of('five'); //display 5 echo $oilINI->get_value_of('ten'); //display 10 //etc etc another option would be define define("my_ini_file", 'oil.ini'); //outside function $lines = file(my_ini_file);//inside function (note no $ ) it really depends on how your planning to use it and how it would expand Quote Link to comment https://forums.phpfreaks.com/topic/213208-var-scopesimple-problemgot-me-beat/#findComment-1110188 Share on other sites More sharing options...
OOP Posted September 12, 2010 Share Posted September 12, 2010 Hi there, in your function, you can refer to the variable $title_path as shown below $lines = file($GLOBALS['title_path']); // without the $ sign regards Quote Link to comment https://forums.phpfreaks.com/topic/213208-var-scopesimple-problemgot-me-beat/#findComment-1110196 Share on other sites More sharing options...
trq Posted September 12, 2010 Share Posted September 12, 2010 Just pass $title_path in as a second arguument to the function. Quote Link to comment https://forums.phpfreaks.com/topic/213208-var-scopesimple-problemgot-me-beat/#findComment-1110210 Share on other sites More sharing options...
scottieclark Posted September 12, 2010 Author Share Posted September 12, 2010 Hi... thanks to you all for the help - great support! OOP...that was it...worked like a charm! Funny eh?..such a little thing, but if you don't know...you just don't know!!! Great - I can get on now... Cheers, S Quote Link to comment https://forums.phpfreaks.com/topic/213208-var-scopesimple-problemgot-me-beat/#findComment-1110331 Share on other sites More sharing options...
trq Posted September 12, 2010 Share Posted September 12, 2010 Don't use globals. Quote Link to comment https://forums.phpfreaks.com/topic/213208-var-scopesimple-problemgot-me-beat/#findComment-1110398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.