Home » Php » In PHP, how does include() exactly work?

In PHP, how does include() exactly work?

Posted by: admin July 12, 2020 Leave a comment

Questions:

How does include('./code.php'); work? I understand it is the equivalent of having the code “pasted” directly where the include occurs, but, for example:

If I have two pages, page1.php and page2.php, of which I would need to manipulate different variables and functions in ./code.php, does include() create a copy of ./code.php, or does it essentially link back to the actual page ./code.php?

How to&Answers:

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';

Answer:

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.

Answer:

It depends on how you include the code.php file. If you include the code.php file into page1.php and then page2.php then you will have access to it’s vars and it would effecitvely just “copy and paste” (beaware that is just used to make it easier to understand since the actual dynamics of that are explained above) the file in however if you link like:

[code.php]
include('page1.php');
include('page2.php');

Then code.php will have access to all of the variables within page1.php but:

  • page1.php will not have access to vars in code.php
  • page2.php will not have access to vars in code.php

So in order to inherit the funcitonality of code.php inot both page1.php and page2.php be sure to do something like:

[page1.php]
include('code.php');

[page2.php]
include('code.php');

Then it will work as you expect. So just something to remember there.

Answer:

Include does not behave like copy paste.

Here is a demonstration using PHP Strict Type Declarations

<?php

// function.php (file)

declare (strict_types = 1);

function sum(int $a, int $b)
{
  return $a + $b;
}

/**
 * This will throw TypeError: 
 */

// echo sum(5.2, 5);

However, if I call this function from an external file

<?php

// caller.php (file)

require_once 'function.php';

/**
 * This will Work instead of throwing a TypeError:
 */

echo sum(5.2, 5);

Strict types are enabled for the file that the declare statement was added (function.php)

It does not apply for any function calls made from an external file (caller.php)
because PHP will always defer to the caller when looking to evaluate strict types.

Require doesn’t work like copy/paste

For further understanding,
Short answer on how PHP script is Executed &
A more comprehensible explanation on what include/require really does in php

Answer:

include('./code.php'); // The Same As Pasting The Code From Code.PHP.

It Will Not Redirect To Code.php In Any Case.