473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

cannot access inc files from a function

I have the following piece of code (symplified)

require("lang.{$lang}.inc.php"); // I include the inc. files
....
function decodify($technique) { // functions that give me back the
value in inc.file
switch ($technique) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}
.....
Somewhere I call the function ....decodify($myTechnique)

But it doen't print the content from $charcoal, $oil or, etc.
Do I have to declare the function global or something like that. In
that case, how should I do it??
Many thanks. Bettina

Jul 29 '05 #1
9 1800
Hallo Bettina,

What is the exact name of the file you are trying to include. Your construct
is very weird, if the file is "lang.EN.inc.php" for example, try:

require("lang.$lang.inc.php");

Also, could you post the code of that included file so we can see if the
values are really there.

Hope this helps,

DXTrim
<be*****@coaster.ch> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have the following piece of code (symplified)

require("lang.{$lang}.inc.php"); // I include the inc. files
...
function decodify($technique) { // functions that give me back the
value in inc.file
switch ($technique) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}
....
Somewhere I call the function ....decodify($myTechnique)

But it doen't print the content from $charcoal, $oil or, etc.
Do I have to declare the function global or something like that. In
that case, how should I do it??
Many thanks. Bettina

Jul 29 '05 #2
be*****@coaster.ch wrote:
I have the following piece of code (symplified)

require("lang.{$lang}.inc.php"); // I include the inc. files
...
function decodify($technique) { // functions that give me back the
value in inc.file
switch ($technique) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}
....
Somewhere I call the function ....decodify($myTechnique)

But it doen't print the content from $charcoal, $oil or, etc.
Do I have to declare the function global or something like that. In
that case, how should I do it??
Many thanks. Bettina


You cannot access a variable that is outside of a function unless you
write global $var; inside the function. The other way is to include the
lang file inside the function.

Gvre
Jul 29 '05 #3
Hi DXTrim,

the values are really there since I use the references outside the
function and it work ok. I think it has something to do with what says
Giannis regarding the global variable. I'll try that. Thank you.

DXTrim schrieb:
Hallo Bettina,

What is the exact name of the file you are trying to include. Your construct
is very weird, if the file is "lang.EN.inc.php" for example, try:

require("lang.$lang.inc.php");

Also, could you post the code of that included file so we can see if the
values are really there.

Hope this helps,

DXTrim
<be*****@coaster.ch> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have the following piece of code (symplified)

require("lang.{$lang}.inc.php"); // I include the inc. files
...
function decodify($technique) { // functions that give me back the
value in inc.file
switch ($technique) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}
....
Somewhere I call the function ....decodify($myTechnique)

But it doen't print the content from $charcoal, $oil or, etc.
Do I have to declare the function global or something like that. In
that case, how should I do it??
Many thanks. Bettina


Jul 29 '05 #4
How should I define the global variable inside this function? What must
be global is the lang.{$lang}.inc.php so that it can be read inside the
function, isn't it?. I tried including the lang file in the function
but I receive an error.

require("lang.{$lang}.inc.php");
function technique($t) {
switch ($t) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}

<? technique($myTechnik ?>

Jul 29 '05 #5
be*****@coaster.ch wrote:
How should I define the global variable inside this function? What must
be global is the lang.{$lang}.inc.php so that it can be read inside the
function require("lang.{$lang}.inc.php");
function technique($t) {
switch ($t) {
case 'C':
echo $charcoal;
...


$charcoal is local to your function. You can do it in two ways..

1.
function technique($t) {
global $charcoal;

switch ($t) {
case 'C':
echo $charcoal;

2.
function technique($t) {
switch ($t) {
case 'C':
echo $GLOBAL['charcoal'];
Jul 29 '05 #6
be*****@coaster.ch wrote:
How should I define the global variable inside this function? What must
be global is the lang.{$lang}.inc.php so that it can be read inside the
function, isn't it?. I tried including the lang file in the function
but I receive an error.

require("lang.{$lang}.inc.php");
function technique($t) {
switch ($t) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}

<? technique($myTechnik ?>


function technique($t) {
global $lang; // here we are using the global var $lang
$incFile = "lang.{$lang}.inc.php";

if ( file_exists($incFile) )
include ($incFile);
else
return;

switch ($t) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}

Gvre

ps. To OP. You can put all strings in file "lang.{$lang}.inc.php" in an
array and access it like this. If you do it like this you don't have to
change the function technique($t) every time you put a new var in lang file.

// vars file
$strings = array (
"C" => "charcoal",
"O" => "oil",
"A" => "acrylic"
);
// index ? file
$incFile = "lang.{$lang}.inc.php";
if ( file_exists($incFile) )
include ($incFile);
else
return; // or any other action

// functions file
function technique($t) {
global $strings;

if (array_key_exists($t, $strings))
echo $strings[$t];

}

Jul 29 '05 #7
Thank you, the first option works ok!

Jul 29 '05 #8
Hallo Giannis, that of putting everything in my lang file so that I
don't have to change the function every time I add a new variable works
fine! Thank you for the tip!

Giannis Vrentzos schrieb:
be*****@coaster.ch wrote:
How should I define the global variable inside this function? What must
be global is the lang.{$lang}.inc.php so that it can be read inside the
function, isn't it?. I tried including the lang file in the function
but I receive an error.

require("lang.{$lang}.inc.php");
function technique($t) {
switch ($t) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}

<? technique($myTechnik ?>


function technique($t) {
global $lang; // here we are using the global var $lang
$incFile = "lang.{$lang}.inc.php";

if ( file_exists($incFile) )
include ($incFile);
else
return;

switch ($t) {
case 'C':
echo $charcoal;
break;
case 'O':
echo $oil;
break;
case 'A':
echo $acrylic;
break;
}
}

Gvre

ps. To OP. You can put all strings in file "lang.{$lang}.inc.php" in an
array and access it like this. If you do it like this you don't have to
change the function technique($t) every time you put a new var in lang file.

// vars file
$strings = array (
"C" => "charcoal",
"O" => "oil",
"A" => "acrylic"
);
// index ? file
$incFile = "lang.{$lang}.inc.php";
if ( file_exists($incFile) )
include ($incFile);
else
return; // or any other action

// functions file
function technique($t) {
global $strings;

if (array_key_exists($t, $strings))
echo $strings[$t];

}


Jul 30 '05 #9
> But it doen't print the content from $charcoal, $oil or, etc.
Do I have to declare the function global or something like that. In
that case, how should I do it??
Many thanks. Bettina


to combine some of the ideas in this thread...
replace the whole case block with:

echo $GLOBAL[array('C'=>'charcoal','O'=>'oil','A'=>'acrylic')[$technique]];
Bye.
Jasen
Aug 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Herring | last post by:
Hi there: Well, let me start off by saying that I am a Visual Studio drag and drop weenie, not a real programmer. So I tend to get confused when things do not look like the instructions said they...
9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Raed Sawalha | last post by:
Hello , I have windows service which do listening to specified directory using FileSystemWatcher , on Created Event and Get all the files in the directory using Directory.GetFiles function then...
0
by: Sin | last post by:
> Xxxxx : error PRJ0008 : Could not delete file 'd:\xxxxxxx.dll'. > Make sure that the file is not open by another process and is not write-protected. > > LINK : fatal error LNK1168: cannot open...
6
by: JonSteng | last post by:
..Net Visual Studio Professional 2003 Version 7.1.3088 ..Net Framework 1.1 SP1 Version 1.1.4322 IIS 5.1 Windows XP Professional SP2 Micron T3000 Laptop (1.5 GHz; 1GB RAM; 40GB HD with 17GB Free)...
10
by: dermot | last post by:
I have wrriten a small windows service application in visual studio ..net 2003 which listens for incoming FTP files. These files would overwrite over time due to duplicate file names. However any...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.