|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2002-06-07 03:10 UTC] sander@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 07:00:01 2025 UTC |
<?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 "$$aVariable" : 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('"', '"', $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="© 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; ?>