473,657 Members | 2,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1823
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

"WindAndWav es" <ac****@ngaru.c om> wrote in message
news:Ta******** ************@ne ws.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

"WindAndWav es" <ac****@ngaru.c om> wrote in message news:Ta******** ************@ne ws.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
18998
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
1
1655
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 seems to block each other. I use something like this : start thread waiting for connexion with : server.threadserver = threading.Thread(target = self.__startserver) then do while 1 : socket, host = accept(...) then start a new thread for each new...
15
2138
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
6677
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 I created funcion CREATE FUNCTION DB2ADMIN.xxx( )
12
1729
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 understanding him. Thank you Wes McCaslin
17
2705
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 pattern1 print >>orcfilename, line I am pretty sure my code isn't close to what I want. I need to be able
13
1716
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 not be at the end of the line starting with ; until the end of the line including white spaces.. this is a corrected version from http://python-forum.org/py/viewtopic.php?t=1703
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8499
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.