PhP Functions and whatnot

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

PhP Functions and whatnot

Post 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!!
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

mmm more info

Post 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... ;)
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

go figure

Post by mikusan »

Well the quickfix was to pass the variables thru index.php as in function connect($host,$username...)

Anyone got a better suggestion?
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post 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.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

...

Post 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
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post 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.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Sweet..

Post by mikusan »

Haven't tried but that shed some light on some things THX!!!
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

YES!!!

Post by mikusan »

WOHAAA That's exactly what it was THANK you very much.... i spent all day in my books/ websites.........
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post 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.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

...

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Post Reply