Edox
Edox

Reputation: 1

Insert into file php code from other php file

I'm trying to insert something like this php code:

<?php echo 'Hello world!'; ?>

using this script:

<?php
$code = '<?php echo "Hello world!"; ?>';
$file = fopen('file.php', w)
fputs($file, $code);
fclose($file);
?>

in another .php file that the server will run when a client request the page. is it possible to do? if it is, how? i know you couldn't do it with this script but I don't know how can I do. I don't need to print Hello word! In the new file I need the exactly code in the variable $code.

Upvotes: 0

Views: 207

Answers (3)

Boopathi Rajan
Boopathi Rajan

Reputation: 1210

try this code

<?php
$code = "<?php echo 'Hello world!'; ?>";
$file = fopen('file.php', 'w');
fputs($file, $code);
fclose($file);
?>

Upvotes: 0

Lkopo
Lkopo

Reputation: 4835

You have two choices:

$code = '<?php echo \'Hello world!\'; ?>';

OR

$code = "<?php echo 'Hello world!'; ?>";

As in the comments you can see, put quotes around w and also ; after fopen():

$file = fopen('file.php', 'w');

Upvotes: 1

montbless
montbless

Reputation: 41

try it

$code = "<?php echo 'Hello world!'; ?>";

you need use double quotes to write this code

Upvotes: 1

Related Questions