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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

undefined variable

Status
Not open for further replies.

SBuzzT

Programmer
Aug 24, 2005
86
CA
OK... It's bee ahwile, but I have a problem with an undefined variable. Whenever I declare a variable on a page to be included on another, the variable comes up as an undefined variable. For example, if I was working on index.php, here is the code I would use:
Code:
<?
  include("test.php");
  echo $abcd;
?>
On test.php, I would do the following:
Code:
<?
  $abcd = "test";
?>
When I run index.php, the error would read:
Code:
Notice: Undefined variable: abcd in index.php on line 1

Any ideas? This used to be fairly simple, but with globals now set to off, it seems like a pain (although more secure). Is there a good way to handle this properly?
 
4.3.1

I think I have the problem narrowed down (partially due to my stupidity)...

The code I am working with is a little more complicated than I posted. The problem is that I am declaring the variable at the top of the test.php, but using much later on the index page. There is a condition that occurs before I call the include that looks for the variable declared in the test.php. But if I require or include it any sooner, the rest of the code on the test.php is placed in the wrong part of index.php. Is there a way to read just the variable declared and then later run the rest of the code on index.php? (make sense?)
 
The code I am working with is a little more complicated than I posted
That's what I suspected.

Is there a way to read just the variable declared and then later run the rest of the code on index.php? (make sense?)
I think I understand. No, using only one included file, there is no way to run only part of the code.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I would suggest to place whatever test.php is outputting later on in the page into a variable in your included file. that way you can just echo the variable wherever you need it. This then allows you to include the file before checking for the existance of the variable, but still control where the rest of the output is to be placed.

for example:

included_file.php
Code:
$somevar="somevalue"
function display_some_html(){
$html_var="<body> This is the html to display...<div>....";
return $html_var;
}

in the main page.php
Code:
include("included_file.php");
if($some_var...){
do something...
}
echo display_some_html();





----------------------------------
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top