Jump to content

$var scope...simple problem...got me beat!


scottieclark

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/213208-var-scopesimple-problemgot-me-beat/
Share on other sites

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

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 :D

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.