Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include_once 3

Status
Not open for further replies.

sen5241b

IS-IT--Management
Sep 27, 2007
199
US
I have a var in an include file called settings.php. At the beginning of a function if I include_once the settings.php file the var is NULL and does not exist but if I just include the file, I can reference the var. Yes, I've read the manual but it doesn't seem to shed light on this issue.
 
I had the same problem and solved it by using the $GLOBALS array.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
it's just scope. in one use of the function the variable will be in the appropriate scope, in the other it won't be.

globals get around the problem but it's better (imo) if you have a basic understanding of the scope of your variables and code appropriately.
 
Without seeing your code its hard to say, but if I do this it works just fine:

Code:
$myvar="This variable is set";

Code:
include_once('settings.php');
if(isset($myvar)){
echo $myvar;
}

else
{
echo "myvar is not set";
}

However if put the include inside a function , and call the function more than once, the variable from the include file is only set the first time I call it.

Code:
[red]function outputvar(){
include_once('settings.php');
 
if(isset($myvar)){
echo $myvar;
}

else
{
echo "myvar is not set";
}
}
[/red]

[green]//Call functiontwice[/green]
outputvar(); [green]\\Outputs var contents[/green]
echo "<hr>Second call<br>";
outputvar();[green]//Outputs "Var is not set"[/green]


Why, because the include statement is only executed once as per the functions name. though it gets called again in the function it still the same script, and as such won't include it again. So at that point the variable is o longer set.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Also if your trying something like this...
Code:
[red]<?[/red]
[maroon]$myvar[/maroon]=[green]"This variable is set"[/green];
[red]?>[/red]
Code:
[red]<?[/red]
[blue]include_once[/blue]([green]'settings.php'[/green]);

[blue]function[/blue] output(){
  [blue]echo[/blue] [maroon]$myvar[/maroon]; [gray]// Fails undefined variable[/gray]
}
output(); [gray]// Will output an error message[/gray]

[blue]echo[/blue] [maroon]$myvar[/maroon]; [gray]// Outputs, This variable is set[/gray]

[gray]// A way around this is ...[/gray]

[blue]function[/blue] output2(){
  [blue]global[/blue] [maroon]$myvar[/maroon];
  [blue]echo[/blue] [maroon]$myvar[/maroon]; [gray]// Outputs, This variable is set[/gray]
}
output2(); [gray]// No error messages, outputs the variable[/gray]
[red]?>[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top