By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

newbe question about including a function

P: n/a
Hi Gurus

I hope I am going to make sense with this question:

I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a
little, mix with html, etc...). Now, I would like to use a function in my php code, as otherwise I have to type the same thing over
and over again. With a function, I mean something along of the lines of:

function x($string) {
return strlen($string);
}
but obviously, the function is a bit more sophisticated.

My question is where should I put the function
a. in a separate file - and how should I link it in that case?
b. at the bottom of the file
c. at the top of the file
d. I should do it differently altogether

I would appreciate some hints. TIA

- Nicolaas
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
WindAndWaves wrote:

My question is where should I put the function
a. in a separate file - and how should I link it in that case?
b. at the bottom of the file
c. at the top of the file


Any of the above may be appropriate. The important think is to have a plan
and stick to it. The overhead of 'including' a file is quite low, but does
present the issue of maintaining a shared file in an environment with no
compilation / build verification.

The rules I apply are usually this:

1) no more than 20 files in any one directory
2) a directory for each 'task'
3) only include files from current directory or from out-of-document-root
library directories
4) no output in include files (except where explicitly invoked from the
including script)
5) include files named with '.inc.php' or '.inc' to differentiate from
normal php files
6) global symbols (function, class names, global vars) in 'library' include
have a consistent prefix unique to that file
7) functions which might be shared (e.g. between data-input and viewing
scripts) defined in an include file
8) php scripts (not include files) structured as follows:
[ comment describing file (php)]
[ includes (php)]
[ declaration of globals / defines (php)]
[ class definitions (php)]
[ function definitions (php)]
[ 'main' (php)]
[ additional html (html)]

You might want to take a look at the Pear style guidelines
(http://pear.php.net/manual/en/standards.php).

HTH

C.

Jul 17 '05 #2

P: n/a
WindAndWaves wrote:
Hi Gurus

I hope I am going to make sense with this question:

I have an html page that I have turned into a php page with a bit of php
code above the html (connect to database, massage data a
little, mix with html, etc...). Now, I would like to use a function in my
php code, as otherwise I have to type the same thing over and over again.
With a function, I mean something along of the lines of:

function x($string) {
return strlen($string);
}
but obviously, the function is a bit more sophisticated.

My question is where should I put the function
a. in a separate file - and how should I link it in that case?
Yes.
look at www.php.net
search for the command include
or include_once
or require

That does excactly what you ask for.
b. at the bottom of the file
Doesn't matter.
Once a function is on the page, PHP will find it.
c. at the top of the file
see b
d. I should do it differently altogether
No, your plan is ok.

I would appreciate some hints. TIA

- Nicolaas

Good luck.

Regards,
Erwin Moller
Jul 17 '05 #3

P: n/a

"WindAndWaves" <ac****@ngaru.com> wrote in message
news:Ta********************@news.xtra.co.nz...
Hi Gurus

I hope I am going to make sense with this question:

I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a little, mix with html, etc...). Now, I would like to use a function in my php code, as otherwise I have to type the same thing over and over again. With a function, I mean something along of the lines of:

function x($string) {
return strlen($string);
}
but obviously, the function is a bit more sophisticated.

My question is where should I put the function
a. in a separate file - and how should I link it in that case?
b. at the bottom of the file
c. at the top of the file
d. I should do it differently altogether

I would appreciate some hints. TIA

- Nicolaas


I usually define my PHP functions in a separate file. Mostly to avoid
confusing them with Javascript functions that appear in the HTML.

The general convention is that include statements appear at the top of a
page before everything else:

<?

require("dingo.php");
require("donkey.php");

?>
<html>
<head><? donkey_headers(); ?>
....

Jul 17 '05 #4

P: n/a

"WindAndWaves" <ac****@ngaru.com> wrote in message news:Ta********************@news.xtra.co.nz...
Hi Gurus

I hope I am going to make sense with this question:

I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a
little, mix with html, etc...). Now, I would like to use a function in my php code, as otherwise I have to type the same thing over and over again. With a function, I mean something along of the lines of:

function x($string) {
return strlen($string);
}
but obviously, the function is a bit more sophisticated.

My question is where should I put the function
a. in a separate file - and how should I link it in that case?
b. at the bottom of the file
c. at the top of the file
d. I should do it differently altogether

I would appreciate some hints. TIA

- Nicolaas


Thank you all VERY VERY much. Much appreciated. It is always useful to read all the online documentation. But affirmations like
yours give me three times the confidence to continue along the long and windy road to becoming a PHP expert (I have just started
walking....)

Thank you again.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.