php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17605 variable variables and double quotes in HEREDOC strings
Submitted: 2002-06-05 05:04 UTC Modified: 2002-06-07 03:10 UTC
From: mbruggem at usal dot es Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.2.0 OS: Winows NT
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mbruggem at usal dot es
New email:
PHP Version: OS:

 

 [2002-06-05 05:04 UTC] mbruggem at usal dot es
<?php
/*
  This program demonstrates the behavior of variable variables
  inside of HEREDOC strings which are later EVALuated.

  SUMMARY: In HEREDOC strings without embeded double quotes variable variables can be
           interpolated by using EVAL. If there are embeded double quotes in a HEREDOC 
           string variable variables are not (further) interpolated by the EVAL function.

*/

$lBN = "\n<br>\n>>>\n";
$rBN = "\n<<<\n<br>\n";

$page_title = "More String tests";
$aString   = "This is a string";
$aVariable = "aString";

$a_single_quoteVar = <<<EOD
<p>
   This is part of a HERE_DOC which does not contain any double quotes:
</p>
<p>
<pre>
    $$aVariable  : a variable variable without any quotes
   '$$aVariable' : a variable variable with single quotes
</pre>
</p>

EOD;

$a_double_quoteVar = <<<EOD
<p>
   This is part of a HERE_DOC which does contain double quotes:
</p>
<p>
   <font color='red'>
         <b>NOTE</b>: The variable variable do not work in any of the cases where previously
         the variable variable had worked in single quotes and without quotes.
   </font>
</p>
<pre>
    $$aVariable  : a variable variable with  no quotes
   '$$aVariable' : a variable variable with single quotes
   "$$aVariable" : a variable variable with double quotes
</pre>
</p>

EOD;

$a_extra_quoteVar = <<<EOD
<p>
   This is part of a HERE_DOC which does not contain double quotes aroung the variables
   but does have some "extra" double quotes - i.e: quotes that do not contain variables
   but just happen to exist inside the string.
</p>
<p>
   <font color='red'>
         <b>NOTE</b>: The mere presense of double quotes inside the string is sufficient
         to cause the variable variable to stop working.
   </font>
</p>
<p>
   '$$aVariable' : a Variable as a variable variable with single quotes
</p>

EOD;

$a_entity_quoteVar = <<<EOD
<p>
   This is part of a HERE_DOC which does contain <b>entity</b> double quotes. It can be used
   as a work around:
</p>
<p>
<pre>
   '$$aVariable' : a Variable as a variable variable with single quotes
   &quot;$$aVariable&quot; : a Variable as a variable variable with entity double quotes
</pre>
</p>

EOD;

/*
//  Uncomment this block to see that all of the pre-EVAL states of the variable variable are the same.

  echo $a_single_quoteVar;
  echo $a_double_quoteVar;
  echo $a_extra_quoteVar;
  echo $a_entity_quoteVar;
*/

eval ("\$a_single_quoteVar = \"$a_single_quoteVar\";");
eval ("\$a_double_quoteVar = \"$a_double_quoteVar\";");
eval ("\$a_extra_quoteVar = \"$a_extra_quoteVar\";");
eval ("\$a_entity_quoteVar = \"$a_entity_quoteVar\";");

$a_entity_quoteVar = ereg_replace('&quot;', '"', $a_entity_quoteVar);

$page_body =   $lBN . $a_single_quoteVar . $rBN
             . $lBN . $a_double_quoteVar . $rBN
             . $lBN . $a_extra_quoteVar  . $rBN
             . $lBN . $a_entity_quoteVar . $rBN;

// eval ("\$page_body = \"$page_body\";");

$page_template =<<<EOD
<html>
<head>
            <title>$page_title</title>
            <meta name="author" content="Martin Bruggeman">
            <meta name="web master" content="Martin Bruggeman">
            <meta name="copyright" content="&copy; 2002 Me, myself, and I">
            <meta name="keywords" content="testing an exam system">
            <style>
                   .main { margin: 2em; padding: 1em; }
            </style>
</head>
<body>

<div CLASS='main'>

<p><a href="$PHP_SELF">come again</a></p>


$page_body

</div>

</body>
</html>
EOD;

print $page_template;

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-07 03:10 UTC] sander@php.net
You need to addslashes() the stuff before you eval() it.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Oct 20 19:01:27 2019 UTC