8

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?

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
John Zimmerman
  • 163
  • 2
  • 7
  • 3
    I'm not sure I follow what you're asking, but what you said at the beginning of your question is accurate. It basically pastes the code in place – Paul Dessert Aug 24 '12 at 23:18
  • 2
    Some explanations about this here http://php.net/manual/en/function.include.php – user710502 Aug 24 '12 at 23:30

5 Answers5

7

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';
uınbɐɥs
  • 7,236
  • 5
  • 26
  • 42
6

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.

Niko
  • 26,516
  • 9
  • 93
  • 110
0

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.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
0

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

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

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

William Beston
  • 347
  • 2
  • 5