-1

I'm trying to create multiple .php files using php itself.
I want to put some code into a file; most of code is the same but only one or two variables that I wanted to be dynamic. I mean every file that I make are exactly like each other the only difference between theme is one variable. My function is this:

function generate_corn_files()
{
        $C = $GLOBALS['C'];
        $db = $GLOBALS['db'];

        //delete all contents of folder
        RemoveDir($C->INCPATH.'cron/feed/', false);
        $res    = $db->query('SELECT id FROM category ');
        while($cat = $db->fetch_object($res)) {
                $id     = $cat->id;

                $open_output = <<<'PHP'
<?php
$outter_id      = $id;
if($example = true){
        echo 'test';
        echo $C->INCPATH;
}

?>
PHP;
                $fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
                fwrite($fp, $open_output);
                fclose($fp);
        }
}

I tried to put content of file using heredoc but I want to $id in $outter_id = $id; be equal to $id = $cat->id;
it's a variable outside of heredoc I can't make it work inside of it !
Are there any other solutions to make it work ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mr.5hady
  • 57
  • 6

2 Answers2

2

You aren't using HEREDOC syntax but rather NOWDOC syntax. If you use HEREDOC, all variables inside will be evaluated, so you will have to escape with \$ the variables you don't want evaluated.

$open_output = <<<PHP
<?php
\$outter_id      = $id;
if(\$example = true){
        echo 'test';
        echo \$C->INCPATH;
}
?>
PHP;

Or, you can stick with NOWDOC, use a placeholder, and replace it afterwards.

$open_output = <<<'PHP'
<?php
$outter_id      = %%%id%%%;
if($example = true){
        echo 'test';
        echo $C->INCPATH;
}
?>
PHP;
str_replace("%%%id%%%", $id, $open_output);
miken32
  • 42,008
  • 16
  • 111
  • 154
1

Maybe this could inspire you

function generate_corn_files()
{
    $C = $GLOBALS['C'];
    $db = $GLOBALS['db'];

    //delete all contents of folder
    RemoveDir($C->INCPATH.'cron/feed/', false);
    $res    = $db->query('SELECT id FROM category ');
    while($cat = $db->fetch_object($res)) {
            $id     = $cat->id;

            $open_output = <<<'PHP'
<?php
$outter_id      = $id;
if($example = true){
    echo 'test';
    echo $C->INCPATH;
}

?>
PHP;

    $php_var_name_pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
    $open_output = preg_replace_callback(
        $php_var_name_pattern, 
        function($matches) {
                if(isset($GLOBALS[$matches[1]])) {
                        if(is_string($GLOBALS[$matches[1]])) { 
                                return '\''.$GLOBALS[$matches[1]].'\'';
                        } else {
                                return $GLOBALS[$matches[1]];
                        }
                } else { 
                        return $matches[0];
                }          
        }, 
        $open_output);

        $fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
            fwrite($fp, $open_output);
            fclose($fp);
    }
}
Radu Dumbrăveanu
  • 1,266
  • 1
  • 22
  • 32
  • thanks ! good answer , but i was wondering isn't there any way to create .php file and fill it with php codes instead of using << – Mr.5hady Oct 20 '15 at 20:14
  • No easy way that won't be a formatting and debugging nightmare as what you are doing is somewhat unusual, this is the best way IMHO. – Wobbles Oct 20 '15 at 20:50
  • Actually you are not using heredoc-syntax, but newdoc-syntax which is equivalent, in some close way, to single-quoted strings, thus why don't you use old-single-quoted strings? – Radu Dumbrăveanu Oct 21 '15 at 05:50