Page 1 of 1

PhP Functions and whatnot

Posted: Thu May 01, 2003 1:48 pm
by mikusan
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!!

mmm more info

Posted: Thu May 01, 2003 2:02 pm
by mikusan
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... ;)

go figure

Posted: Thu May 01, 2003 2:42 pm
by mikusan
Well the quickfix was to pass the variables thru index.php as in function connect($host,$username...)

Anyone got a better suggestion?

Posted: Thu May 01, 2003 3:11 pm
by Sevengraff
Im having trouble understanind the problem. It would help if you post error messages that you get.

and using global variables would probally fix the connect() function you have there.

...

Posted: Thu May 01, 2003 3:32 pm
by mikusan
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

Posted: Thu May 01, 2003 3:51 pm
by Sevengraff
yeah, using a global variable would probally fix it.

config.php:

Code: Select all

<?
$host = "localhost";
$user = "user";
$pass = "pass";
?>
function.php:

Code: Select all

<?
include("config.php");
function connect() {
global $host, $user, $pass;
$conn = mysql_connect($host, $user, $pass);
}
?>
normally what i do is just have all those things in one file, global.php, and just include that where it's needed.

Sweet..

Posted: Thu May 01, 2003 4:22 pm
by mikusan
Haven't tried but that shed some light on some things THX!!!

YES!!!

Posted: Thu May 01, 2003 5:18 pm
by mikusan
WOHAAA That's exactly what it was THANK you very much.... i spent all day in my books/ websites.........

Posted: Thu May 01, 2003 5:22 pm
by Sevengraff
yeah, that was a big snag for me when i started making functions too.
but there are some vars that you don't need to use "global" to get access to. the $_POST and $_GET ones are visable everywhere.

...

Posted: Thu May 01, 2003 5:35 pm
by mikusan
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.

Posted: Thu May 01, 2003 5:42 pm
by volka