Disappearing PHP Variables

Hello, I'm working on a peice of PHP code which produces a separate PHP document that contains HTML code with some PHP along side. When this document is created it displays just like a HTML page and the PHP within it works properley most of the time.

The problem is when I use variables in this created document, because when I save a document with PHP variables they don't appear in the created document when I open it.

The created document does work when I put the PHP variables in manually later on, but I don't understand why I can't use PHP to create a document which contains PHP variables. I've tried using double quotes and putting curly braces around the variable names but that doesn't work. Here is the code I'm talking about:

PHP:

// Creating the about page file name based on the user input
$aboutPageFileName = $_POST['name'];

// Creating the content that will go inside the about page file
$aboutPageContent=
"
<html>
<head>
</head>


<body style='font-family: Verdana'>

<!-- Importing the text of the current file name used for the 'about' page text (the name of the about file could change, i.e. environment.txt, cars.txt) -->
<input type='text' value='<?php $fileName = file_get_contents('Info/currentAboutFileName.txt'); $file = file_get_contents($fileName); echo $file; ?>' width='200' height='200' style='left:0px; top:60px;'>

</body>

</html>
";


// Saving the About Page
$aboutFileObject = fopen("Info/"."$aboutPageFileName.php", "w");
fwrite($aboutFileObject, $aboutPageContent);
fclose($aboutFileObject);// Destroying file object (file has been saved)

// Creating a file that contains the name of the current 'about' file (containing the text for the about page)
$aboutNameFileObject = fopen("Info/"."currentAboutFileName.txt", "w");
fwrite($aboutNameFileObject, $aboutPageFileName);
fclose($aboutNameFileObject);// Destroying file object (file has been saved)


Created HTML and PHP:

<html>
<head>
</head>


<body style='font-family: Verdana'>

<!-- Importing the text of the current file name used for the 'about' text (the name of the about file could change, i.e. environment.txt, cars.txt) -->
<input type='text' value='<?php = file_get_contents('Info/currentAboutFileName.txt'); = file_get_contents(); echo ; ?>' width='200' height='200' style='left:0px; top:60px;'>
------------------------------------> Variable names disappear in the created document
</body>

</html>


I've tried searching what the problem could be but I can't seem to find anything about this problem I have in particular, so thanks to anyone who can solve it.
Last edited on
PHP likes to automatically substitute variable names with their values in strings - try using single quotes instead of double quotes.
Thanks, this worked for me. I was trying to save a PHP file with text that displayed both the variable names and the variable values, and so I stored the code for both in separate variables using double quotes for displaying variable values and single quotes for displaying variable names. I then joined the two variables in the fwrite() function, i.e. fwrite($fileObject, $displayVariableNames.$displayVariableValues). This method allows you to create new variables in the created PHP file as well as embed the variable values into the PHP file from the file creation code (i.e. a timestamp).
Last edited on
Topic archived. No new replies allowed.