472,975 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 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 4553
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Larry Sankey | last post by:
Hello. I am having what is probably the simplest problem there is with Nusoap. I have to admit I'm new to this stuff, but I have some programming experience and what I'm doing *shouldn't* be hard....
0
by: NotGiven | last post by:
Below is a code snippet I am having a hard time with. I rerquire code1.php and code 2.php. code1.php contains some variables the code needs. code2.php contains some functions I call from the...
5
by: mostof | last post by:
I'm facing a problem with require_once... Instead of actually including the file i'm requiring, it just dumps it out as text... other functions are working quite well... the code look like...
13
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
3
by: Sean Quinn | last post by:
Hi, I don't know if anyone has run into similar problems, but it seems like when I use `require_once(...)' with files that contain functions I get an error indicating that it can't redeclare the...
11
by: Kimmo Laine | last post by:
I'm flipping my wig here, people. I'm using classes and making each class a file. when I'm including dependet classess, I use require_once to avoid multiple declarations - yet they happen. I put...
3
by: mich dobelman | last post by:
I cannot reference the values in side of config.php $dbhost,$dbuser ,$dbpasswd ,$dbname why? <?php require_once('includes/db.php'); //db connection functions require_once('config.php'); ...
6
by: Shelly | last post by:
Here is a crazy question that has happend to me once before. I have an include file for the connection information to the server. It is like this: $hostname= "the_server_location"; $database...
1
by: Zeba | last post by:
Hi ! I have been given a php codebase. For now I have installed php plugin into my eclipse so that I can work with my java files at the same time. My problem is this: All the php files in...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.