Page 1 of 1

include variable.

Posted: Thu Nov 30, 2006 8:19 am
by Ruud Hermans
I included a file where I have certain variables in I would like to use in a different file. But the following I'm trying refuses to work.

I created the file called extra.php

Code: Select all

$number = "nummer";
Then I included it in the top of the file index.php before any of the content is displayed.

Code: Select all

<?php
include("extra.php");
?>
Where I wanted to use the variable in the file I placed the code:

Code: Select all

<?php echo "$number"?>
The only thing this shows me now is a blank space. Could some one please explain to me what I am doing wrong?

Posted: Thu Nov 30, 2006 8:31 am
by JayBird
Is the file called English or Extra?

What you said above doesn't correlate and would cause the problem

Posted: Thu Nov 30, 2006 8:33 am
by Ruud Hermans
JayBird wrote:Is the file called English or Extra?

What you said above doesn't correlate and would cause the problem
I'm sorry both files carry the same name. I simukated the situation so it would be more easy to understand.

Posted: Thu Nov 30, 2006 9:36 am
by volka

Code: Select all

<?php // include.php
$xyz = 'abc';
?>

Code: Select all

<?php
require 'include.php';

if (!isset($xyz)) {
	echo 'you're using a strange php version.';
}
else {
	echo $xyz;
}
?>

Posted: Thu Nov 30, 2006 10:09 am
by Luke
volka - you forgot to escape the apostrophe :wink:

Posted: Thu Nov 30, 2006 10:26 am
by Ruud Hermans
For some reason I still can't get this all to work. Should I put all the data inside the same php tags?

Far as I see it you changed "include" for "require" so I edited that part witch had no effect. I also tried to edit it by inserting the require in front of the place I would like to see the variable.

Just to make it more easy to explain at least I hope. I don't want to use this in a if/else statement. The variable holds a word that should be placed at the location the variable is inserted.

*Maby some usefull info: The variable is called inside a html table.

Posted: Thu Nov 30, 2006 11:12 am
by RobertGonzalez
file1.php

Code: Select all

<?php
$this_var = 'I am included';
?>
file2.php

Code: Select all

<?php
include 'file1.php';

if (!isset($this_var))
{
    $this_var = 'I am nowhere to be found...';
}

echo $this_var;
?>
Put both of these files in the same folder. Then call file.php and see what comes out. The 'if' check is only checking to see if include worked. This is for testing.