PhP Functions and whatnot
Moderator: General Moderators
PhP Functions and whatnot
Okay i have this one problem that will make the difference between 200 lines of code and 50... just so say...
okay, i have 2 files. one is called index.php and another one functions.php. When i load a website index.php is read (DUH) and require_once('filepath/functions.php'); is used.
Now in functions.php there is one function, that "attempts" to open 'read' a file and store it into an array, however, the code that opens the file cannot be in the function.php and only when i move that file() command to the index.php will my website work.
To better explain myself... i am making a template function, that will read a file with some html and some tags, my index.php calls the function and replaces the tags with something. however, to make it work, i must open the file in index.php and then call the function. Which sux when you have 5 templates to call...
I have exactly the same problem while trying to make a dunction called DB_connect, if i put it into a function that is in the functions.php and call it from the index.php, it would not run...
Any help would be killer!!
okay, i have 2 files. one is called index.php and another one functions.php. When i load a website index.php is read (DUH) and require_once('filepath/functions.php'); is used.
Now in functions.php there is one function, that "attempts" to open 'read' a file and store it into an array, however, the code that opens the file cannot be in the function.php and only when i move that file() command to the index.php will my website work.
To better explain myself... i am making a template function, that will read a file with some html and some tags, my index.php calls the function and replaces the tags with something. however, to make it work, i must open the file in index.php and then call the function. Which sux when you have 5 templates to call...
I have exactly the same problem while trying to make a dunction called DB_connect, if i put it into a function that is in the functions.php and call it from the index.php, it would not run...
Any help would be killer!!
mmm more info
I am a noobie in PhP but not in programming, i see that my functions.php has an include to the config.php file where all the paths and constants are defined, HOWEVER, the config.cfg file is also require_once thing...
now what should i do about it... define the variales global after i rewuire the config.cfg...or what...
the format looks like this: Say config.cfg has a variable $user
[functions.php]
require_once($path . '/config.php');
function connect()
{
$connect = mysql_connect($host, $user, $pass)
}
Help plez...
now what should i do about it... define the variales global after i rewuire the config.cfg...or what...
the format looks like this: Say config.cfg has a variable $user
[functions.php]
require_once($path . '/config.php');
function connect()
{
$connect = mysql_connect($host, $user, $pass)
}
Help plez...
- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
...
I was hoping to be as detailed as possible, but okay, in a nutshell...
i have a config.php file with a bunch of constants.
i require_once config.php (don't mind hte syntax) in functions.php
in functions.php i have a function that USES one of those variables, However, i have found that that is not the case. Everything works fine, and the variables are there IF and only IF i don't use those variables in a function.
this really bugs me...
thx
i have a config.php file with a bunch of constants.
i require_once config.php (don't mind hte syntax) in functions.php
in functions.php i have a function that USES one of those variables, However, i have found that that is not the case. Everything works fine, and the variables are there IF and only IF i don't use those variables in a function.
this really bugs me...
thx
- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
yeah, using a global variable would probally fix it.
config.php:
function.php:
normally what i do is just have all those things in one file, global.php, and just include that where it's needed.
config.php:
Code: Select all
<?
$host = "localhost";
$user = "user";
$pass = "pass";
?>Code: Select all
<?
include("config.php");
function connect() {
global $host, $user, $pass;
$conn = mysql_connect($host, $user, $pass);
}
?>- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
...
Now, now that you have defined it as a global variable, if i write another function in the same file do i have to call the variable global again... also from C, C++, or java, you could set a variable global, and never have to clal it INSIDE the function, if you know what i mean... one of my futile attempts was to like call it global just at the beginning of the file... outside of the function.