473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

newbe question about including a function

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
4 1794
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: Sebastien GIRAUD | last post by:
Hello, First let say that am french and that i'll try to write the best english i can... I'm a python newbe and have the following problem : I try to create 2 threads in a server program and they...
15
by: JC Home | last post by:
Hi all, I am just learning JavaScript and would love to find a good editor that helps with syntax and debugging. Any suggestions? Thanks!! -- Jeff Ciaccio Dallas, GA
9
by: Yaro | last post by:
Hello DB2/NT 8.1.3 Sorry for stupid questions. I am newbe in DB2. 1. How can I read *.sql script (with table and function definitions) into a database? Tool, command... 2. In Project Center...
12
by: Wes McCaslin | last post by:
I have a question about concrete and abstract classes. what are the differances? can you explain them to me. I have started an advance Visual Studio.Net class and I am having a hard time...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.