473,386 Members | 1,602 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,386 software developers and data experts.

Calling functions from an included file

mreeves000
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.

7 3668
Markus
6,050 Expert 4TB
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
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
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
431 256MB
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
I'm pretty sure you need to include the file before you use functions from it.
Mar 26 '10 #6
Dormilich
8,658 Expert Mod 8TB
@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
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

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

Similar topics

11
by: juglesh | last post by:
at one centraldomain.com, I have central.php, which consists of this: <?php function square($num) { return $num * $num; } ?> at outerdomain.com, I have test.php, which consists of this:...
1
by: Me Padre | last post by:
To all, I have an Intranet that I have developed which uses templates and all ASP functions in separate included ASP files. For instance, functions.asp gets included in each page and does...
6
by: Charlie | last post by:
Ok, I have three files: calc.cpp, calc.h, and ui.cpp. I would like to call a function that is located in ui.cpp from main() in calc.cpp. Both files have calc.h included, but when I tried to...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
4
by: wanwan | last post by:
Hi, I have a project with multiple c files. Some of these modules have functions that have the same name and their operations are exactly the same. My rusty memory tells me there is a way...
6
by: Richard | last post by:
I am new to php and am looking for some quick "get up and go" help. What would the arguments for and against be for function declarations v simple include? e.g <?php include("myfunc.php");...
16
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
4
by: Dan | last post by:
Hi All, I've got a problem with my C++ application that calls a Java class that I've built with GCJ, I can't run it because I get errors: multiple definition of `atexit' first defined here...
16
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've...
8
by: Derek Hart | last post by:
I am unclear about what all the requirements are to call a simple vb.net application, installed in the GAC, from COM (such as writing vba in Word to call the dotnet dll). I believe I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.