Cleaned up simple string variable parsing example, as per bug #53254.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@305306 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Justin Martin 2010-11-13 03:46:44 +00:00
parent cfb33420f6
commit 9adf7eba60

View file

@ -513,14 +513,20 @@ EOT;
<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 @@ echo "He drank some {$beer}s"; // works
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.