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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.