Php include not working? function not being included

Tom picture Tom · Apr 23, 2011 · Viewed 94.3k times · Source

Here is the full context of the situation:

I recently got a new Mac, I'm a php developer so I downloaded MAMP and started developing.

First I noticed that my includes were not being included, but I changed that by configuring my php.ini.

However now, when I try to include a file with a function it does not recognize the function.

For example I have a file named functions.php:

<?php
function doit(){
    echo "did it";
}
?>

and a file that includes it called index.php

<?php include("functions.php"); doit();?>

and I get this error message

Fatal error: Call to undefined function doit() in index.php on line 4

Answer

Sean Adkinson picture Sean Adkinson · Apr 23, 2011

Sometimes the current directory isn't what you expect it to be, such as when you include a file from an included file.

I like to use $_SERVER['DOCUMENT_ROOT'] on my includes so that I can always reference them absolutely from the root of my site:

<?php
    include($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");
    doit();
?>

If your includes directory is above your document root, you can use .. to still reference from the root.