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 - <?php
-
function clean($data)
-
{
-
$data = str_replace("&", "&", $data);
-
$data = str_replace("'", "'", $data);
-
$data = str_replace("\"", """, $data);
-
return $data;
-
}
-
?>
-
-
body.php uses this code to call the function
-
<?
-
$title = clean($_POST['title']);
-
?>
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.
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: - 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
You need to include() the files. -
<?php
-
include '/path/to/functions.php';
-
// or require
-
require '/path/to/functions.php';
-
-
// now you can call clean()
-
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.
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 - <?
-
$title = clean($_POST['title']);
-
?>
-
<html>
-
<head></head>
-
<body>
-
<? require ("../includes/header.php");?>
-
-
(the code in the header causes the code to find the functions.php file from anywere on the site)
-
header.php
-
<?
-
$functions = "includes/functions.php";
-
$found2 = false;
-
do
-
{
-
if (file_exists($functions))
-
{
-
$found2=true;
-
include_once ($functions);
-
}
-
else
-
{
-
$functions = "../" . $functions;
-
}
-
}while(!$found2);
-
?>
-
-
functions.php
-
<?php
-
function clean($data)
-
{
-
$data = str_replace("&", "&", $data);
-
$data = str_replace("'", "'", $data);
-
$data = str_replace("\"", """, $data);
-
return $data;
-
}
-
?>
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: - include_once ("http://your.site.com/includes/functions.php");
Which, on the plus side, eliminates the need to go poking around for the file.
I'm pretty sure you need to include the file before you use functions from it.
@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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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:...
|
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...
|
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...
|
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...
|
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...
|
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");...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |