This is a very strange approach and it is not necessary to do so.
But if you really want to do this, then:
- Before
require declare a variable, for example, $codePart = "part_1" ; - File
index.php format as follows:
if (!empty($codePart)) { switch ($codePart) { case 'part_1': $text = 'Привет'; echo $text; break; case 'part_2': $day = 21; echo $day; break; } }
In second.php you will have something like:
$codePart = 'part_1'; require 'index.php';
In fact, the file will be included all, but only the selected part will be displayed. This approach is bad and dead end.
It is much better (although not also optimally) to put the blocks you need into separate functions, and then just call them where necessary.
index.php: <?php function partOne() { echo 'Привет'; } function partTwo() { echo 21; } second.php: <?php include('index.php'); partOne(); //выводится "Привет" partTwo(); //выводится 21
So you will have the file included exactly once, and the blocks can be output where necessary.
Better yet, read about template engines.
eval()- lampa