For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2006 > load global variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author load global variables
alexjaquet@gmail.com

2006-05-23, 6:58 pm

Hi,

I want to store some global variable into an hash table then I can
access it by naming the key :
$SERVER{'var1'}

in perl for example I do :

sub loadLanguage {
$lang = $query->param("lang");
$lang = uc ($lang);
open (FILE, "<$dirLang/$lang.conf") or die "cannot open file
$dirLang/$lang.conf";

while (<FILE> ) {
(local our $label, local our $value) = split(/=/);
$SERVER{$label} = $value;
}
close (FILE);
}

for loading in $SERVER the language variables values then I can access
it simply :
print "$SERVER{'some_test'} "

How can I do that in php

thanks for it

alexjaquet@gmail.com

2006-05-23, 6:58 pm

I'm trying to do the following , but the file isn't open :s any idea ?

<?php
$lang = $_GET['lang'];
$LABEL;
loadLanguage ();

function loadLanguage () {
$handle = fopen("C:\\Apache2\\htdocs\\recordz\\lang\\.$lang.conf",
"r") ;
while (!feof($handle)){
$text = fgets($handle);
list($key,$value) = explode("=", $text);
$LABEL{$key} = $value;
}
fclose($handle);
}
?>

alexjaquet@gmail.com

2006-05-23, 6:58 pm

Correction :

<?php
$lang = $_GET['lang'];
$LABEL = loadLanguage ($lang);

function loadLanguage ($lang) {
$handle = fopen(" C:\\Apache2\\htdocs\\recordz\\lang\\$lan
g.conf",
"r") ;
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
list ($key,$value) = explode("=", $buffer);
$LABEL{$key} = $value;
}
fclose($handle);
}
return $LABEL;
}

?>

tim.n.hunt@gmail.com

2006-05-23, 6:58 pm


alexjaquet@gmail.com wrote:
> Correction :
>
> <?php
> $lang = $_GET['lang'];
> $LABEL = loadLanguage ($lang);
>
> function loadLanguage ($lang) {
> $handle = fopen(" C:\\Apache2\\htdocs\\recordz\\lang\\$lan
g.conf",
> "r") ;
> if ($handle) {
> while (!feof($handle)) {
> $buffer = fgets($handle, 4096);
> list ($key,$value) = explode("=", $buffer);
> $LABEL{$key} = $value;


Hi

Use $LABEL[$key], array access in php is always with [].

All arrays in php's are associative arrays so for perl users the syntax
for using associative arrays in php is more like using an perl array
not a hash.

Anyone whos use perl before php must get caught by this one...

Everything else in your script looks right.

By the way, comp.lang.php is busier than alt.comp.lang.php, you might
get a quicker reply there

Tim

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com