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

Calling functions from an included file

mreeves000
P: 3
Greetings,
Ok so, what I am trying to do is have one file that stores several functions so I can use them across the entire site.
So first I have the page, in every page I call header.php, and inside that I call functions.php
But when I try to run the function I get this error:
PHP Fatal error: Call to undefined function clean()

Since I am pretty sure it is being caused simply becuase the function in inside an include (I have tested and found this to be the case as the function works just fine if placed in the body page) I will keep my examples to just the calling of the function and the function.php file.
So my pages are setup like this:
functions.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function clean($data)
  3. {
  4.     $data = str_replace("&", "&amp;", $data);
  5.     $data = str_replace("'", "&apos;", $data);
  6.     $data = str_replace("\"", "&quot;", $data);
  7.     return $data;
  8. }
  9. ?>
  10.  
  11. body.php uses this code to call the function
  12. <?
  13. $title = clean($_POST['title']);
  14. ?>
I need the functions in a seprate file as I will probably need to edit them but I need to call it from about 6 to 10 different pages.
Any help would be appriciated.
Mar 25 '10 #1

✓ answered by eragon

It appears here that functions.php is being included within header.php which is being included after you call your function clean();. Therefore, the function clean(); doesn't actually exist until the body of your page, where you include header.php.

If you want your file to be included regardless of what page you are on, consider instead of a path relative to the document, a path relative to the site's URL, eg:

Expand|Select|Wrap|Line Numbers
  1. include_once ("http://your.site.com/includes/functions.php");
Which, on the plus side, eliminates the need to go poking around for the file.

Share this Question
Share on Google+
7 Replies


Markus
Expert 5K+
P: 6,050
You need to include() the files.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include '/path/to/functions.php';
  3. // or require
  4. require '/path/to/functions.php';
  5.  
  6. // now you can call clean()
  7.  
Mar 25 '10 #2

P: 50
Adding to Markus's post it is better to use include_once and require_once on files that contain functions that will be used through multiple files the speed difference between the two functions is not anything to really be worried about.
Mar 25 '10 #3

mreeves000
P: 3
I already have the functions.php file included in my page. and I still receive the error. Plus I am sure the include is working because I can get an Echo to output to the page from functions.

ok So a little more indepth,

body.php
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $title = clean($_POST['title']);
  3. ?>
  4. <html>
  5. <head></head>
  6. <body>
  7. <? require ("../includes/header.php");?>
  8.  
  9. (the code in the header causes the code to find the functions.php file from anywere on the site)
  10. header.php 
  11. <?
  12. $functions = "includes/functions.php";
  13. $found2 = false;
  14. do
  15. {
  16.     if (file_exists($functions))
  17.     {
  18.         $found2=true;
  19.         include_once ($functions);
  20.     }
  21.     else
  22.     {
  23.         $functions = "../" . $functions;
  24.     }
  25. }while(!$found2);
  26. ?>
  27.  
  28. functions.php
  29. <?php
  30. function clean($data)
  31. {
  32.     $data = str_replace("&", "&amp;", $data);
  33.     $data = str_replace("'", "&apos;", $data);
  34.     $data = str_replace("\"", "&quot;", $data);
  35.     return $data;
  36. }
  37. ?>
Mar 25 '10 #4

eragon
100+
P: 431
It appears here that functions.php is being included within header.php which is being included after you call your function clean();. Therefore, the function clean(); doesn't actually exist until the body of your page, where you include header.php.

If you want your file to be included regardless of what page you are on, consider instead of a path relative to the document, a path relative to the site's URL, eg:

Expand|Select|Wrap|Line Numbers
  1. include_once ("http://your.site.com/includes/functions.php");
Which, on the plus side, eliminates the need to go poking around for the file.
Mar 26 '10 #5

P: 50
I'm pretty sure you need to include the file before you use functions from it.
Mar 26 '10 #6

Dormilich
Expert Mod 5K+
P: 8,639
@eragon
on the other hand side, it is good practice to include every required (library) file at the beginning of the page.

additionally, putting those files in a publicly not accessible directory adds to your safety. additionaly it is best to call files using their absolute path.
Mar 26 '10 #7

mreeves000
P: 3
Thanks Everyone. I don't kow what I was thinking yesterday.
Eragon is completely right, an included file is not part of the page until after the include has run, and my call was happening before that.
Mar 26 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.