How to Write PHP to a .php File

Okay, so I’m hoping this is a stupid question: How do I write PHP to a file? It seems that most of the examples I find is writing plain text to a .txt file (and I even wrote html to a webpage file). I need to write PHP to a .php file so that it can access a database. How would I do that? I can’t add PHP code to a variable to pass to the file writing functions…


$code = '

file_put_contents('some-file.php', $code);

It can be done and IMHO it gets easier with practice.

The trick is you need to escape anything that PHP might interpret into a string before writing it to the file. eg.

write \<?php \$var

Wouldn’t those need to be single quotes?

fixed :slight_smile:

Escaping string is definitely going to pose problems.

I didn’t necessarily mean escaping the string, but escaping the characters that PHP would parse into a string ie. so PHP won’t parse them prior to writing to the file.

But single quotes does seem the easiest way.