SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,762
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)

    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.
    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
    Code:
    <?php ="URLTITLEHERE";
    ="Web Design";
    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?
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Hi Ryan,

    I haven't test this but it sounds like it would work. Do this with the variables:
    Code:
    $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);
    
    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
    ictus==""

  3. #3
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,762
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    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.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  4. #4
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,762
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    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.
    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);
    ?>
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  5. #5
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    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:
    Code:
    $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:
    Code:
    $string =<<<TLJsdak
     ...
    TLJsdak;
    Glad you got it sorted out.

    Steve
    ictus==""


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •