在脚本之间包含来自另一个文件的php代码

What I'm trying to do is fairly simply, but does not seem work.

In this main page, I'm trying to insert a piece of code from another page like so.

<?php

include_once $_SERVER['DOCUMENT_ROOT'].'\attach\common.php';

$clear = False;

echo $code;

if($clear){
    echo 'Clear was cleared';
}

?>

This is common.php

<?php

$code = "if(isset($_GET['n'],$_GET['c'])) {";
$code .="   echo 'Name and Country were set <br />';";
$code .="   $clear = True;";
$code .="} else {";
$code .="   echo 'An error occured <br />';";
$code .="}";

?>

How can I do this in php?

Update

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'\attach\common.php';
$clear = False;
d();
if($clear){
    echo 'Clear was cleared';
}
?>

<?php

function d() {
    if(isset($_GET['n'],$_GET['c'])) {
        echo 'Name and Country were set';
        $clear = True;
    } else {
        echo 'An error occured <br />';
    }
}
?>

Please be careful using eval() as it's a very dangerous way of achieving what you want.

Why can't you just put code in common.php instead of putting it up as a string inside variable?

i.e. common.php:

<?php
if(isset($_GET['n'],$_GET['c'])) {
   echo 'Name and Country were set <br />';
   $clear = true;
} else {
   echo 'An error occured <br />';
}
?>

-

In the case where you'd really need to do so:

Use eval() to parse $code. That's because your code is a string variable and not actual code.

<?php

include_once $_SERVER['DOCUMENT_ROOT'].'\attach\common.php';

$clear = False;

eval($code);

if($clear){
    echo 'Clear was cleared';
}

?>

You don't put code in a string to include it in another file, just put the code as it is.

common.php should be

<?php

if(isset($_GET['n']) && isset($_GET['c'])) {
echo 'Name and Country were set <br />';
$clear = True;
} else {
   echo 'An error occured <br />';
}

?>