Using vars in functions?

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
User avatar
xjake88x
Forum Commoner
Posts: 50
Joined: Sun Aug 01, 2004 7:05 pm

Using vars in functions?

Post by xjake88x »

Topic makes this sound noob, but I'm having a problem:

In my content management system JakeSys ([url=http://www.designsbyjake.net/jakesys/]demo[url]), I have certain functions such as check_level, smilestohtml, htmltosmiles, etc, and I want to use variables from config.php in those functions. Here is the structure of header.php that is in every page:

Code: Select all

//start session etc

include("config.php");

//bunch-of-code
//I can use variables from config.php here!

function check_level($level){
//function code
//If I use a variable from config.php, it's just blank, like it's not set!
}
The problem I'm having is in the comments. Thanks if you can help :-D.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You can do the following:

Code: Select all

<?php
//start session etc 

include("config.php"); 

//bunch-of-code 
//I can use variables from config.php here! 

function check_level($level){ 
global $myVar,$myVar2;
//function code 
//If I use a variable from config.php, it's just blank, like it's not set! 
} 
?>
or if your variable in config.php never change set them as constants.

Code: Select all

<?php
define('MY_VAR','my var');
?>
User avatar
xjake88x
Forum Commoner
Posts: 50
Joined: Sun Aug 01, 2004 7:05 pm

Post by xjake88x »

Thank you -- I'll just set them as constants! I use constants in C++ & VB but they're different.

If, for example, I use DEFINE('perm_post_news','2'), would I be able to get the value by using $perm_post_news?

Also, thanks for responding in less than 60 seconds :D.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

No prob.

Code: Select all

<?php
define('PERM_POST_NEWS','2'), 
echo PERM_POST_NEWS;
?>
I like to define my constants in all caps, makes it easier for me. But not required.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Also:

Will store a string:

Code: Select all

<?php
define('PERM_POST_NEWS','2'), 
?>
Will store an integer:

Code: Select all

<?php
define('PERM_POST_NEWS',2), 
?>
User avatar
xjake88x
Forum Commoner
Posts: 50
Joined: Sun Aug 01, 2004 7:05 pm

Post by xjake88x »

:-D Ok so I can i use a $ though.. because theres probably hundreds of instances of the config variables as variables in my other files..

What does global $myvar etc do? Can I be like:

Code: Select all

function check_level($req_lvl){
	global $redir_to_login; //This variable is set in config.php
	if($_SESSION['level']>=$req_lvl){
		return true;
	}else{
		if($redir_to_login){
			echo "<script language="JavaScript">";
			echo "location.href="index.php?page=login";";
			echo "</script>";
			return false;
		}else{
			echo "You lack permission to use access this page.<br />";
			echo "Your current level is <strong>".$_SESSION['level']."</strong> (".$_SESSION['level_name'].").<br \>";
			echo "The required level level is <strong>".$req_lvl."</strong> (".level_name($req_lvl).").<br \>";
			return false;
		}
	}
}
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

xjake88x wrote::-D Ok so I can i use a $ though.. because theres probably hundreds of instances of the config variables as variables in my other files..
For global variables you use a $ for [php_man]constants[/php_man] you dont.
User avatar
xjake88x
Forum Commoner
Posts: 50
Joined: Sun Aug 01, 2004 7:05 pm

Post by xjake88x »

So I'd just global them in config.php?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if they are set outside a function, or set through global inside a function, you just need to global them inside your next function.. or pass them in (which is generally better).. depending on the data/usage
Post Reply