Results 1 to 5 of 5

Thread: External PHP Function Not Found

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    External PHP Function Not Found

    I'm still pretty new to PHP but I'm finding it to be a very powerful and useful language. However, I'm finding a very critical need for a feature that I've been told is available but I can't seem to get it to work properly.

    So PHP has a language construct for including external PHP scripts into others, similar to include directives in C. While I've been able to use this include statement properly, what I've been finding is that functions located in this included script aren't accessible by the including file for some reason.

    The domain where I'm having this problem at is within an HTML file. I need to conditionally modify the style and text of an element by retrieving certain information from a database when the page loads. The code within the PHP file to connect to and retrieve the data from the database works perfectly fine when it's not encapsulated within a function but I ideally would need this code placed within a function to be used elsewhere within the page source.

    So for example, the PHP file would have something like this:

    Code:
    <?php
        function someFunction() {
            // DB connection/extraction code here
            return requestedInformation;
        }
    ?>
    And then within the nested HTML file (with a .php extension obviously):

    Code:
    <?php include 'php_filename.php' ?>
    <?php $var = someFunction(); ?>
    The second line throws an error with regards to an undefined function.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: External PHP Function Not Found

    Perhaps an obvious question but need to ask: if you take the include out of the implementation (ie., if you take the function out of the included file and put it in the same file as where you use it), does it work as expected?

    If so, try changing "include" to "require" and see if it throws a "failed to open file" error. If it does, then your include can't find the file, but your error reporting isn't configured to mention it.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: External PHP Function Not Found

    Quote Originally Posted by GamerMax5 View Post
    The domain where I'm having this problem at is within an HTML file.
    Am I to assume that this means you're trying to use PHP within an HTML file (as in, a file with the extension htm or html)? If so, then this isn't working because HTML isn't PHP. You could modify your webserver (easily with htaccess using Apache) to make PHP run HTML files, but by default it won't.
    Like Archer? Check out some Sterling Archer quotes.

  4. #4

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: External PHP Function Not Found

    Am I to assume that this means you're trying to use PHP within an HTML file (as in, a file with the extension htm or html)?
    That assumption is incorrect. While I'm sure the sentence I wrote had the wrong impact (entirely my fault for not being clear enough and I apologize),

    And then within the nested HTML file (with a .php extension obviously):
    rest assured that the HTML markup lies within a PHP file. Otherwise, as you stated, having PHP directives inside the markup wouldn't work because Apache isn't configured to parse pure HTML as PHP (at least mine isn't).

    Perhaps an obvious question but need to ask: if you take the include out of the implementation (ie., if you take the function out of the included file and put it in the same file as where you use it), does it work as expected?
    So oddly enough it works fine when the function is removed from the external source file and placed into the same file in which I make the call from.

    I have already tried replacing include with require to see if that had any other difference and it did not. I know require would have killed the execution of the source had it encountered an error but it doesn't (and I was hoping it would actually because then I'd be getting somewhere).

    I've provided two different types of links to the PHP source to these directives and both have not thrown an error: either a direct path on the server (/server/page_files/etc/...) or a URL to the source file. In both cases, the include or require directives have not thrown the error, rather the call to the function throws the error and I still can't figure this one out.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  5. #5
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: External PHP Function Not Found

    Hmm, not sure what's wrong. Could you try the following setup for the sake of eliminating some potential factors? Create these these two files in the same directory:

    File: test1.php
    PHP Code:
    <?php
        
    function someFunction() {
            return 
    5;
        }
    ?>
    File: test2.php
    PHP Code:
    <?php include 'test1.php' ?>
    <?php $var 
    someFunction(); ?>
    <?php
    echo $var// 5
    ?>
    Then try running test2.php and see what happens.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width