Patch variable-parsing-simpleexample-fix for Documentation problem Bug #53254
Patch version 2010-11-11 22:00 UTC
Return to Bug #53254 |
Download this patch
Patch Revisions:
Developer: frozenfire@php.net
Index: en/language/types/string.xml
===================================================================
--- en/language/types/string.xml (revision 305170)
+++ en/language/types/string.xml (working copy)
@@ -513,14 +513,20 @@
<programlisting role="php">
<![CDATA[
<?php
-$beer = 'Heineken';
-echo "$beer's taste is great"; // works; "'" is an invalid character for variable names
-echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer"
-echo "He drank some ${beer}s"; // works
-echo "He drank some {$beer}s"; // works
+$juice = "apple";
+
+echo "He drank some $juice juice.".PHP_EOL;
+echo "He drank some juice made of $juices."; // Invalid. "s" is a valid character for a variable name, but the variable is $juice.
?>
]]>
</programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+He drank some apple juice.
+He drank some juice made of .
+]]>
+ </screen>
</informalexample>
<simpara>
@@ -530,49 +536,48 @@
object properties as to simple variables.
</simpara>
- <informalexample>
+ <example><title>Simple syntax example</title>
<programlisting role="php">
<![CDATA[
<?php
-// These examples are specific to using arrays inside of strings.
-// When outside of a string, always quote array string keys and do not use
-// {braces}.
+$juices = array("apple", "orange", "koolaid1" => "purple");
-// Show all errors
-error_reporting(E_ALL);
+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;
+echo "He drank some $juices[koolaid1] juice.".PHP_EOL;
-$fruits = array('strawberry' => 'red', 'banana' => 'yellow');
+class people {
+ public $john = "John Smith";
+ public $jane = "Jane Smith";
+ public $robert = "Robert Paulsen";
+
+ public $smith = "Smith";
+}
-// Works, but note that this works differently outside a string
-echo "A banana is $fruits[banana].";
+$people = new people();
-// Works
-echo "A banana is {$fruits['banana']}.";
-
-// Works, but PHP looks for a constant named banana first, as described below.
-echo "A banana is {$fruits[banana]}.";
-
-// Won't work, use braces. This results in a parse error.
-echo "A banana is $fruits['banana'].";
-
-// Works
-echo "A banana is " . $fruits['banana'] . ".";
-
-// Works
-echo "This square is $square->width meters broad.";
-
-// Won't work. For a solution, see the complex syntax.
-echo "This square is $square->width00 centimeters broad.";
+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.";
?>
]]>
-<!-- XXX this won't work:
-echo "This square is $square->{width}00 centimeters broad.";
-// XXX: php developers: it would be consequent to make this work.
-// XXX: like the $obj->{expr} syntax outside a string works,
-// XXX: analogously to the ${expr} syntax for variable var's.
--->
</programlisting>
- </informalexample>
+ &example.outputs;
+ <screen>
+<![CDATA[
+He drank some apple juice.
+He drank some orange juice.
+He drank some juice made of s.
+He drank some purple juice.
+John Smith drank some apple juice.
+John Smith then said hello to Jane Smith.
+John Smith's wife greeted Robert Paulsen.
+Robert Paulsen greeted the two .
+]]>
+ </screen>
+ </example>
<simpara>
For anything more complex, you should use the complex syntax.
|