471,306 Members | 863 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

simple require_once / config / functions problem

i would like my program to have a config.php with passwords and other
stuff.

i would like to have a functions.php to hold all my functions.

i would like the functions.php to include/require the config.php as it
will need it for database stuff.

i cant seem to get the functions in my functions.php to see the
variables in config.php
when i run the following on my machine i just get notices about the
undefined variables. what is wrong with this?

thanks for any help!!
here is the basic layout:

index.php:
<?php
require_once "functions.php";
myfunction();
?>

functions.php:
<?php
require_once "config.php";

function myfunction()
{
print("user: $user , server: $server , pass: $pass ");
}
?>

config.php:
<?php
$user = "root";
$server = "localhost";
$pass = "pass";
?>
Jul 17 '05 #1
2 4451
will taubin <fu***************@hotmail.com> wrote:
i cant seem to get the functions in my functions.php to see the
variables in config.php

here is the basic layout:

index.php:
<?php
require_once "functions.php";
myfunction();
?>

functions.php:
<?php
require_once "config.php";

function myfunction()
{
print("user: $user , server: $server , pass: $pass ");
}
?>

config.php:
<?php
$user = "root";
$server = "localhost";
$pass = "pass";
?>


Read here about variable scope (especially in functions):
http://uk.php.net/manual/en/language...bles.scope.php

HTH;
JOn
--
Sharks are as tough as those football fans who take their shirts off
during games in Chicago in January, only more intelligent.
-- Dave Barry, "Sex and the Single Amoeba: What Every
Teen Should Know"

Jul 17 '05 #2
will taubin wrote:
i would like my program to have a config.php with passwords and other
stuff.

i would like to have a functions.php to hold all my functions.

i would like the functions.php to include/require the config.php as it
will need it for database stuff.

i cant seem to get the functions in my functions.php to see the
variables in config.php
when i run the following on my machine i just get notices about the
undefined variables. what is wrong with this?

You're using the variables defined in config.php _inside_ the function.
That turns them out of scope.
see http://www.php.net/manual/en/languag...bles.scope.php

Either declare them global within the function, or (better, I think)
make them constants in config.php

declaring the variables global:
<?php // functions.php
function my_function() {
global $user, $server, $pass;
print("user: $user , server: $server , pass: $pass ");
}
?>

making them constants:
<?php // config.php
declare('USER', 'root');
declare('SERVER', 'localhost');
declare('PASS', 'pass');
?>

<?php // functions.php
function my_function() {
echo 'user: ', USER, ' , server: ', SERVER, ' , pass: ', PASS, ' ';
}
?>

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Larry Sankey | last post: by
reply views Thread by NotGiven | last post: by
13 posts views Thread by LRW | last post: by
11 posts views Thread by Kimmo Laine | last post: by
6 posts views Thread by Shelly | last post: by
1 post views Thread by Zeba | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.