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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC