Calling functions from an included file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mreeves000
    New Member
    • Mar 2010
    • 3

    Calling functions from an included file

    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

    Code:
    <?php
    function clean($data)
    {
    	$data = str_replace("&", "&amp;", $data);
    	$data = str_replace("'", "&apos;", $data);
    	$data = str_replace("\"", "&quot;", $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.
    Last edited by Markus; Mar 25 '10, 04:24 PM. Reason: Added [code] tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You need to include() the files.

    Code:
    <?php
    include '/path/to/functions.php';
    // or require
    require '/path/to/functions.php';
    
    // now you can call clean()

    Comment

    • philipwayne
      New Member
      • Mar 2010
      • 50

      #3
      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.

      Comment

      • mreeves000
        New Member
        • Mar 2010
        • 3

        #4
        Sorry if I wasn't clear enough

        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
        Code:
        <?
        $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("&", "&amp;", $data);
        	$data = str_replace("'", "&apos;", $data);
        	$data = str_replace("\"", "&quot;", $data);
        	return $data;
        }
        ?>
        Last edited by Dormilich; Mar 26 '10, 06:33 AM. Reason: Please use [code] tags when posting code

        Comment

        • eragon
          Contributor
          • Mar 2007
          • 431

          #5
          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:

          [code=php]include_once ("http://your.site.com/includes/functions.php") ;[/code]

          Which, on the plus side, eliminates the need to go poking around for the file.

          Comment

          • philipwayne
            New Member
            • Mar 2010
            • 50

            #6
            I'm pretty sure you need to include the file before you use functions from it.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by eragon
              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:

              [code=php]include_once ("http://your.site.com/includes/functions.php") ;[/code]

              Which, on the plus side, eliminates the need to go poking around for the file.
              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.

              Comment

              • mreeves000
                New Member
                • Mar 2010
                • 3

                #8
                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.

                Comment

                Working...