By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

modify string in one position only

P: n/a
Is it possible to modify a string in one place using an indexing operator
and an assignment statement?

E.g.:
$s = "1234567890";
$s[5] = "d"; // $s is now "12345d7890"

According to _PHP Bible_ (2nd ed., p.175), this kind of syntax can work ,
but is undocumented and, moreover, appears to be discouraged because "almost
all PHP string manipulation functions return modified copies of their string
arguments rather than making direct changes, which seems to indicate that
this is the style that the PHP designers prefer."

I did a google search (both www and USENET) and found nothing on this.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"sinister" <si******@nospam.invalid> wrote:
Is it possible to modify a string in one place using an indexing
operator and an assignment statement?

E.g.:
$s = "1234567890";
$s[5] = "d"; // $s is now "12345d7890"


Hi,

Square brackets here are deprecated since 4.0, use curly braces:

$s = "1234567890";
$s{5} = "d"; // $s is now "12345d7890"

HTH;
JOn
Jul 17 '05 #2

P: n/a

"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4...
"sinister" <si******@nospam.invalid> wrote:
Is it possible to modify a string in one place using an indexing
operator and an assignment statement?

E.g.:
$s = "1234567890";
$s[5] = "d"; // $s is now "12345d7890"
Hi,

Square brackets here are deprecated since 4.0, use curly braces:

$s = "1234567890";
$s{5} = "d"; // $s is now "12345d7890"


My question wasn't really about {} or []; I already knew the difference.

The line
$s{5} = "d";
(that is, altering a string by an indexing operator and an assignment
statement) isn't documented anywhere either.

HTH;
JOn

Jul 17 '05 #3

P: n/a
"sinister" <si******@nospam.invalid> wrote:
"Jon Kraft" <jo*@jonux.co.uk> wrote:
"sinister" <si******@nospam.invalid> wrote:
> Is it possible to modify a string in one place using an indexing
> operator and an assignment statement?
>
> E.g.:
> $s = "1234567890";
> $s[5] = "d"; // $s is now "12345d7890"


Square brackets here are deprecated since 4.0, use curly braces:

$s = "1234567890";
$s{5} = "d"; // $s is now "12345d7890"


My question wasn't really about {} or []; I already knew the difference.

The line
$s{5} = "d";
(that is, altering a string by an indexing operator and an assignment
statement) isn't documented anywhere either.


Your question was whether it was possible.

From the manual:
"Characters within strings may be accessed by specifying the zero-based
offset of the desired character after the string in curly braces."

http://uk.php.net/manual/en/language.types.string.php

Although the example given shows how to read certain characters in a
string, the term "accessed" doesn't necessarily imply read-only.

JOn
Jul 17 '05 #4

P: n/a

"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:Xn**************************@130.133.1.4...
"sinister" <si******@nospam.invalid> wrote:
"Jon Kraft" <jo*@jonux.co.uk> wrote:
"sinister" <si******@nospam.invalid> wrote:

> Is it possible to modify a string in one place using an indexing
> operator and an assignment statement?
>
> E.g.:
> $s = "1234567890";
> $s[5] = "d"; // $s is now "12345d7890"

Square brackets here are deprecated since 4.0, use curly braces:

$s = "1234567890";
$s{5} = "d"; // $s is now "12345d7890"
My question wasn't really about {} or []; I already knew the difference.

The line
$s{5} = "d";
(that is, altering a string by an indexing operator and an assignment
statement) isn't documented anywhere either.


Your question was whether it was possible.

From the manual:
"Characters within strings may be accessed by specifying the zero-based
offset of the desired character after the string in curly braces."

http://uk.php.net/manual/en/language.types.string.php

Although the example given shows how to read certain characters in a
string, the term "accessed" doesn't necessarily imply read-only.


That's simply not clear. That they don't give an example of a write isn't
encouraging. Nor do they illustrate this operation anywhere else in the
manual, AFAICT.

I do appreciate your having commented, however.

JOn

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.