In PHP, how does include() exactly work?

See the manual.

If you include a text file, it will show as text in the document.

include does not behave like copy-paste:

test.php

<?php
echo '**';
$test = 'test';
include 'test.txt';
echo '**';
?>

test.txt

echo $test;

The example above will output:

**echo $test;**

If you are going to include PHP files, you still need to have the PHP tags <?php and ?>.

Also, normally parentheses are not put after include, like this:

include 'test.php';

Basically, when the interpreter hits an include 'foo.php'; statement, it opens the specified file, reads all its content, replaces the "include" bit with the code from the other file and continues with interpreting:

<?php
echo "hello";

include('foo.php');

echo "world";

becomes (theoretical)

<?php
echo "hello";

?>
{CONTENT OF foo.php}
<?php

echo "world";

However, all this happens just in the memory, not on the disk. No files are changed or anything.

Tags:

Php