How to prevent my variables from being echoed?

This isn’t what my real example is like online but I couldn’t really use that for testing. I’m not going to get into the details but I basically created this example from the code in my page which is cuasing hte issues.

I have this code.

<?php
$fh = fopen("TESTPAGE.php", 'w') or die("can't open file");
$type = "Web Design";
$URLtitle="URLTITLEHERE";
$URL="URLHERE";
$dateTime="timeHERE";
$imgURL="imgurl";
$contentOfPost="CONTENT OF POST IS HERE BLAH BLAH";

$stringData= "<?php $pageIdentity=\\"".$URLtitle."\\";
$articleType=\\"".$type."\\";
include(\\"../../settings.php\\");
?>
<div id=\\"wrapper\\">
	<?php include(\\"../../header.php\\");?>
	<div id=\\"main\\">
		<?php include(\\"../../menu.php\\");?>
		<div id=\\"page\\">
				<div id=\\"content\\">
					<div class=\\"post\\">							
						<h2 class=\\"title article\\"><a href=\\"".$URL."\\">".$urlTitle."</a></h2>
						<p class=\\"meta\\">Posted on ".$dateTime."&nbsp;&bull;&nbsp; <a href=\\"http://www.ryanreese.us/blog/".$URL."\\" class=\\"permalink\\">Full article</a></p>
						<div class=\\"entry\\">
							<p><img src=\\"".$imgURL."\\" width=\\"186px\\" height=\\"186px\\" alt=\\"\\" class=\\"alignleft border\\" />".$contentOfPost."</p>
						</div>
					</div>
				</div>
<?php include (\\"../../sidebar.php\\");?>
		</div>
	</div>
	<?php include(\\"../../footer.php\\");?>
</div>
</body>
</html>";

fwrite($fh, $stringData) or die("cant");

fclose($fh);
?>

When I run that, it opens up a page (creates) and writes that stuff in there. Now, when I go to the page

<?php [B]="URLTITLEHERE";
="Web Design";[/B]
include("../../settings.php");
?>
<div id="wrapper">
	<?php include("../../header.php");?>
	<div id="main">
		<?php include("../../menu.php");?>
		<div id="page">
				<div id="content">
					<div class="post">							
						<h2 class="title article"><a href="URLHERE"></a></h2>
						<p class="meta">Posted on timeHERE&nbsp;&bull;&nbsp; <a href="http://www.ryanreese.us/blog/URLHERE" class="permalink">Full article</a></p>
						<div class="entry">
							<p><img src="imgurl" width="186px" height="186px" alt="" class="alignleft border" />CONTENT OF POST IS HERE BLAH BLAH</p>
						</div>
					</div>
				</div>
<?php include ("../../sidebar.php");?>
		</div>
	</div>
	<?php include("../../footer.php");?>
</div>
</body>
</html>

Notice at the top. The variables names weren’t echoed there. How can I rectify this?

[SIZE=2][FONT=trebuchet ms]Hi Ryan,

I haven’t test this but it sounds like it would work. Do this with the variables:


$page = null;
$page =
  '$type = "Web Design";
   $URLtitle="URLTITLEHERE";
   $URL="URLHERE";
   $dateTime="timeHERE";
   $imgURL="imgurl";
   $contentOfPost="CONTENT OF POST IS HERE BLAH BLAH";';

$page .=<<<HTML
<div id="wrapper"> 
 some test's content in the HTML file here <span class='something'>Something</span>
</div>
HTLM;
$fh = fopen("TESTPAGE.php", 'w') or die("can't open file");
fwrite($fp, $page);
fclose($fp);[COLOR=#007700]
[/COLOR]

The problem was that PHP was interpreting the variables as variables so you need to ensure they are a string before writing it to the file.

Regards,
Steve[/FONT][/SIZE]

Sorry if I’m being dense but I tried adapting your code to my example just now but nothing was written into the page. Could you incorporate your code into mine? I’m not a real big PHP kinda guy and I don’t want my page (online) to be ruined.

And I thought I was writing them as a string. I had quotes around it so I hoped it’d be identified as quotes.

Herp derp. Just changed it to single quotes instead of the double (for the string) and updated the rest of the string accordingly, and it worked, at least in my test example.

Tomorrow morning I’ll update my real code (and publish an article I have ready) and I’ll see if this permanently fixes it.


<?php 
$fh = fopen("TESTPAGE.php", 'w') or die("can't open file");
$type = "Web Design";
$URLtitle="URLTITLEHERE";
$URL="URLHERE";
$dateTime="timeHERE";
$imgURL="imgurl";
$contentOfPost="CONTENT OF POST IS HERE BLAH BLAH";

$stringData= '<?php $pageIdentity="'.$URLtitle.'";
$articleType="'.$type.'";
include("../../settings.php");
?>
<div id="wrapper">
	<?php include("../../header.php");?>
	<div id="main">
		<?php include("../../menu.php");?>
		<div id="page">
				<div id="content">
					<div class="post">							
						<h2 class="title article"><a href="'.$URL.'">"'.$urlTitle.'"</a></h2>
						<p class="meta">Posted on '.$dateTime.'&nbsp;&bull;&nbsp; <a href="http://www.ryanreese.us/blog/'.$URL.'" class="permalink">Full article</a></p>
						<div class="entry">
							<p><img src="'.$imgURL.'" width="186px" height="186px" alt="" class="alignleft border" />'.$contentOfPost.'</p>
						</div>
					</div>
				</div>
<?php include ("../../sidebar.php");?>
		</div>
	</div>
	<?php include("../../footer.php");?>
</div>
</body>
</html>';

fwrite($fh, $stringData) or die("cant");

fclose($fh);
?>

Yup you need that single quote… variables still echo and are interpreted (in this case by your file handler as variables when enclosed in double quotes. A single quoted entity can only be a string.

if you had a mixture of single and double quotes you could use heredoc formatting to create the string like:


$sting = null;
$string =<<<HTML
'$color' = '<p class="my_class">This is a paragraph. It's my best one ever</p>
HTML;



Whatever you choose to encapsulate the heredoc; in this example <<<HTML and HTML; you need to make sure that HTML; is on its’ own line with no spaces. Choosing this would be equally valid:


$string =<<<TLJsdak
 ...
TLJsdak;

Glad you got it sorted out.

Steve