include variable.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ruud Hermans
Forum Newbie
Posts: 11
Joined: Tue Nov 28, 2006 8:33 pm

include variable.

Post 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?
Last edited by Ruud Hermans on Thu Nov 30, 2006 8:34 am, edited 2 times in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Is the file called English or Extra?

What you said above doesn't correlate and would cause the problem
Ruud Hermans
Forum Newbie
Posts: 11
Joined: Tue Nov 28, 2006 8:33 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
}
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

volka - you forgot to escape the apostrophe :wink:
Ruud Hermans
Forum Newbie
Posts: 11
Joined: Tue Nov 28, 2006 8:33 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply