simple require_once / config / functions problem

This is a discussion on simple require_once / config / functions problem within the PHP Language forums, part of the PHP Programming Forums category; i would like my program to have a config.php with passwords and other stuff. i would like to have ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-04-2003
will taubin
 
Posts: n/a
Default 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";
?>
Reply With Quote
  #2 (permalink)  
Old 11-04-2003
Jon Kraft
 
Posts: n/a
Default Re: simple require_once / config / functions problem

will taubin <funk_master_flash@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"

Reply With Quote
  #3 (permalink)  
Old 11-04-2003
Pedro
 
Posts: n/a
Default Re: simple require_once / config / functions problem

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:55 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0