Page 1 of 1

Using vars in functions?

Posted: Wed Aug 04, 2004 5:23 pm
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.

Posted: Wed Aug 04, 2004 5:26 pm
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');
?>

Posted: Wed Aug 04, 2004 5:29 pm
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.

Posted: Wed Aug 04, 2004 5:31 pm
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.

Posted: Wed Aug 04, 2004 5:33 pm
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), 
?>

Posted: Wed Aug 04, 2004 5:40 pm
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;
		}
	}
}

Posted: Wed Aug 04, 2004 5:45 pm
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.

Posted: Wed Aug 04, 2004 5:46 pm
by xjake88x
So I'd just global them in config.php?

Posted: Wed Aug 04, 2004 5:53 pm
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