2

What I was trying to do (and could, actually but only on my local testserver) is to output a php-file with php.
The problem seems to be the opening and closing tags of php.
I looked up nowdoc in php.net but could not find a clue to the solution. If I use it like this:

$fh = fopen($filenameandpath, 'w') or die("can't open file");
$stringData = <<<'WWR'
<?php
echo('test');
?>
WWR;
$suc = fwrite($fh, $stringData);
fclose($fh);

I get a parsing error:

Parse error: syntax error, unexpected T_SL in /home/ua000154/public_html/test.php on line XX

The linenumber of this parsing error is always where the opening php-tag is found. My problem seems to be that I need to escape this tag (I presume I should do the same with the closing tag)

How could I do this? This actually worked on my test server but would not once I uploaded it to the final location on another server.

Any help is apreciated. Thanks you for your time!

Yahel
  • 37,023
  • 22
  • 103
  • 153
leugim
  • 564
  • 1
  • 5
  • 14
  • is it complete file I mean have u added – shankhan Jan 26 '11 at 06:07
  • I would have suspected it's an unsupported edge case, but your example works fine for me. Are you sure your server is running PHP>5.3 ? – mario Jan 26 '11 at 06:07
  • @shankman, Yes it is a complete file with starting and ending tag @mario, I just noticed my remote server is running 5.2.17 and does therfore not support NOWDOC. I used nowdoc because I needed the exact string as output. Now I have to figure out how to escape every variable in the string. Could you help me? – leugim Jan 26 '11 at 06:22

2 Answers2

4

If you're intentionally using NOWDOC syntax, make sure your PHP server is running 5.3 or later, since that was when it was introduced. (You can check using phpinfo();). That would explain why it worked on your dev server but not on production.

Yahel
  • 37,023
  • 22
  • 103
  • 153
  • @yc This is valid syntax for nowdoc (quotes around) – shankhan Jan 26 '11 at 06:03
  • thanks for your quick reply. I am actually using NOWDOC which is the single-quoted version of a php string. This means by placing the quotes no variables inside the nowdoc are interpreted. I need the output to include variables without interpreting them. – leugim Jan 26 '11 at 06:05
  • Ah! that was the missing piece. The remote server is running 5.2. Thanks yc. How could I escape php variables ($variable) inside the heredoc? – leugim Jan 26 '11 at 06:13
  • 1
    @You would need to escape them with a backslash. ie, if `$foo='bar';` then within a traditional HEREDOC, `\$foo` would display `$foo`, while `$foo` would display `bar`. – Yahel Jan 26 '11 at 06:22
  • @everyone: allright! Thanks! Really apreciate the speed and depth of all your responses! @yc Thanks again. Unbelievably fast. – leugim Jan 26 '11 at 06:25
1

Ok, so this is an old question that has been answered, however this may be of use for anyone who wants to implement a similar concept to NOWDOC in earlier versions of php. it uses output buffering to capture text verbatim from the source file, without any variable parsing etc. ie not a lot of use if you WANT to insert variables, but you can literally put anything in it, as long as it does not include the characters "?>", which terminates it.

note that it differs from HEREDOC and NOWDOC that used >>>TERMINATOR and >>>'TERMINATOR' in that the variable is defined after the document.

<?PHP

    function NOWDOC_() {
        ob_start();
    }

    function _NOWDOC(&$buf=false) {
        $buf_ = ob_get_contents();
        ob_end_clean();
        if ($buf!==false) $buf .= $buf_;
        return $buf_;
   }



   NOWDOC_(); ?>random garbage, not shown, but captured into $myvar 


   it has all sorts ] [* \%&  of characters in it

   and completely ignores things like {$this} or $_SERVER['REMOTE_ADDR';

   <?PHP _NOWDOC($myvar);





   NOWDOC_(); ?><HTML><HEAD></HEAD>

   <BODY>Here is some <B>nice</B> HTML &amp; .

     <SCRIPT>

      alert("javascript!");

     </SCRIPT>  

           this also demonstrates using $var = _NOWDOC() syntax.


   </BODY>

   </HTML><?PHP $myhtml = _NOWDOC();


   echo "the html will be [".$myhtml."]";

?>
unsynchronized
  • 4,828
  • 2
  • 31
  • 43
  • just realized i had not correctly allowed for appending to an existing string (my original code did not have the $buf argument as optional, and my edit for this post was a little too hasty. – unsynchronized Sep 20 '12 at 04:20