Expand $str[42] syntax info a little more, $str{42} deprecated as of PHP 6,

and adjust example accordingly


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@212308 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Philip Olson 2006-05-02 18:05:54 +00:00
parent 8b2c8751c0
commit 79349659b2

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.164 $ -->
<!-- $Revision: 1.165 $ -->
<chapter id="language.types">
<title>Types</title>
@ -1084,13 +1084,14 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips";
<para>
Characters within strings may be accessed and modified by specifying the
zero-based offset of the desired character after the string
in curly braces.
using square array-brackets like <varname>$str[42]</varname> so think of
a string as an <type>array</type> of characters.
</para>
<note>
<simpara>
For backwards compatibility, you can still use array-brackets
for the same purpose. However, this syntax is deprecated as
of PHP 4.
For backwards compatibility, you may still use braces like
<varname>$str{42}</varname> for the same purpose. However, this syntax
is deprecated as of PHP 6.
</simpara>
</note>
<para>
@ -1101,19 +1102,22 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips";
<?php
// Get the first character of a string
$str = 'This is a test.';
$first = $str{0};
$first = $str[0];
// Get the third character of a string
$third = $str{2};
$third = $str[2];
// Get the last character of a string.
$str = 'This is still a test.';
$last = $str{strlen($str)-1};
$last = $str[strlen($str)-1];
// Modify the last character of a string
$str = 'Look at the sea';
$str{strlen($str)-1} = 'e';
$str[strlen($str)-1] = 'e';
// Deprecated syntax with braces
$third = $str{2};
?>
]]>
</programlisting>