note 103680 added to language.types.string
| From: | Ultimater at gmail dot com | Date: | Wed, 27 Apr 2011 22:18:51 +0000 |
| Subject: | note 103680 added to language.types.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179211@lists.php.net to get a copy of this message | ||
If you require a NowDoc but don't have support for them on your server -- since your PHP
version is less than PHP 5.3.0 -- and you are in need of a workaround, I'd suggest using
PHP's __halt_compiler() which is basically a knock-off of Perl's __DATA__ token if you are
familiar with it.
Give this a run to see my suggestion in action:
<?php
//set $nowDoc to a string containing a code snippet for the user to read
$nowDoc = file_get_contents(__FILE__,null,null,__COMPILER_HALT_OFFSET__);
$nowDoc=highlight_string($nowDoc,true);
echo <<<EOF
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>NowDoc support for PHP < 5.3.0</title>
<meta name="author" content="Ultimater at gmail dot com" />
<meta name="about-this-page"
content="Note that I built this code explicitly for the
php.net documenation for demonstrative purposes." />
<style type="text/css">
body{text-align:center;}
table.border{background:#e0eaee;margin:1px auto;padding:1px;}
table.border td{padding:5px;border:1px solid #8880ff;text-align:left;
background-color:#eee;}
code ::selection{background:#5f5color:white;}
code ::-moz-selection{background:#5f5;color:white;}
a{color:#33a;text-decoration:none;}
a:hover{color:rgb(3,128,252);}
</style>
</head>
<body>
<h1 style="margin:1px auto;">
<a
href="http://php.net/manual/en/language.types.string.php#example-77">
Example #8 Simple syntax example
</a></h1>
<table class="border"><tr><td>
$nowDoc
</td></tr></table></body></html>
EOF;
__halt_compiler()
//Example code snippet we want displayed on the webpage
//note that the compiler isn't actually stopped until the semicolon
;<?php
$juices = array("apple", "orange", "koolaid1" =>
"purple");
echo "He drank some $juices[0] juice.".PHP_EOL;
echo "He drank some $juices[1] juice.".PHP_EOL;
echo "He drank some juice made of $juice[0]s.".PHP_EOL; // Won't work
echo "He drank some $juices[koolaid1] juice.".PHP_EOL;
class people {
public $john = "John Smith";
public $jane = "Jane Smith";
public $robert = "Robert Paulsen";
public $smith = "Smith";
}
$people = new people();
echo "$people->john drank some $juices[0] juice.".PHP_EOL;
echo "$people->john then said hello to $people->jane.".PHP_EOL;
echo "$people->john's wife greeted $people->robert.".PHP_EOL;
echo "$people->robert greeted the two $people->smiths."; // Won't work
?>
----
Server IP: 69.147.83.197
Probable Submitter: 76.90.130.197
----
Manual Page -- http://www.php.net/manual/en/language.types.string.php
Edit -- https://master.php.net/note/edit/103680
Del: integrated -- https://master.php.net/note/delete/103680/integrated
Del: useless -- https://master.php.net/note/delete/103680/useless
Del: bad code -- https://master.php.net/note/delete/103680/bad+code
Del: spam -- https://master.php.net/note/delete/103680/spam
Del: non-english -- https://master.php.net/note/delete/103680/non-english
Del: in docs -- https://master.php.net/note/delete/103680/in+docs
Del: other reasons-- https://master.php.net/note/delete/103680
Reject -- https://master.php.net/note/reject/103680
Search -- https://master.php.net/manage/user-notes.php