passing Variables with require

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
blast_Stu
Forum Newbie
Posts: 3
Joined: Fri Feb 23, 2007 11:09 am

passing Variables with require

Post by blast_Stu »

Hi there,
I've just started trying to write my login script with Sessions (a different issue!), however I've put the function to check for login in a seperate php file called functions.php

In this file I write to a variable like this:

$loggedIn = true;

The php file is used in, for example, index.php using

require 'functions.php';

I can call the methods of the functions.php file, however index.php does not seem to see the $loggedIn variable.

Are variables not passed in with the require construct? How would I recode such that I can set the variable in the functions.php file and then just access is from the other php files?


Thanks in advance

Regards
Stu
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Are you accessing or setting this variable within a function? If so then the problem is with your variable scope, read up on it at http://www.php.net.
blast_Stu
Forum Newbie
Posts: 3
Joined: Fri Feb 23, 2007 11:09 am

Post by blast_Stu »

Begby wrote:Are you accessing or setting this variable within a function? If so then the problem is with your variable scope, read up on it at http://www.php.net.
I thought it could be the variable scope, so I changed the code such that it ends up like:

$loggedIn = false;

function foo(){
//stuff
$loggedIn = true;

}


This should mean that the variable does not fall out of scope should it not?

Will check php.net though, cheers!


Regards
Stu
blast_Stu
Forum Newbie
Posts: 3
Joined: Fri Feb 23, 2007 11:09 am

Post by blast_Stu »

and the answer appears to be where you told me that it would be! lol

I guess i should RTFM :)


Regards
Stu
Post Reply