SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Calling a function
-
Nov 9, 2006, 15:21 #1
- Join Date
- Apr 2005
- Location
- London, UK
- Posts
- 506
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Calling a function
Hi guys
Is it possible to call a function in different file? If yes how?
cheers
-
Nov 9, 2006, 15:23 #2
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
a function needs to be defined before php can use it. once it is defined, it can be used anywhere in the script.
look at require_once() which is a variation of include()
its common practice to have a dedicated file for function definitions.
-
Nov 9, 2006, 15:23 #3
Yes it is.
You call it the same as you would any other function. Except at the top you have to do an include or a require of the file that contains the function.
PHP Code:include '/Functions/PHPFunctionCodeThingy.php';
-
Nov 9, 2006, 15:31 #4
- Join Date
- Apr 2005
- Location
- London, UK
- Posts
- 506
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok, say we have this code
foo.php
PHP Code:<?
function foo() {
$x=1, $y=2
echo $x + $y;
}
//calling function
foo();
?>
thanks
-
Nov 9, 2006, 15:40 #5
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:require_once 'your_functions_file.php';
foo();
-
Nov 9, 2006, 15:47 #6
Note that if you have the function call in the included file (just like you do in foo.php), you don't need to call it after you include the file. Look at it as concatenating the two files.
Saul
-
Nov 9, 2006, 15:48 #7
- Join Date
- Apr 2005
- Location
- London, UK
- Posts
- 506
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by clamcrusher
-
Nov 9, 2006, 15:54 #8
- Join Date
- Apr 2005
- Location
- London, UK
- Posts
- 506
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by php_daemon
cheers
Bookmarks