Page 1 of 2
code readability question
Posted: Wed Nov 24, 2004 9:46 pm
by rehfeld
which method do you prefer to isolate a character in a string?
Code: Select all
<?php
// we want to isolate "c" out of the following string
$string = 'abcdef';
// method 1
echo $string[2];
// method 2
echo $string{2};
?>
ive always used method 1(prob because i found out how to do that by accident and just kept using it),
but was wondering what peoples thoughts were on which would be better?
one argument i can definately see in favor of method 2,
is that it would be less likely for someone viewing the code to think that $string is datatype array
thoughts?
Posted: Wed Nov 24, 2004 10:48 pm
by nigma
I prefer method #1 because when I think of a string I think of an array of characters. And, when dealing with arrays you use the square brackets for access to individual elements in the array, not the curly brackets.
Posted: Wed Nov 24, 2004 11:06 pm
by jason
I would avoid using method 1. $string is a string, not an array. If in the course of reading your code, I saw echo $string[20], or something like that, it would make perfect sense for me to use $string as an array.
It's not an array. Using {} is just as acceptable, and much clearer.
Basically: $string is NOT an array. Don't make your code look like it is.
Posted: Thu Nov 25, 2004 12:11 am
by jl
Personally I'd use substr(), but out of those two methods as far as readability goes 1 is definitely the best method because [] is the standard array index enclosure characters for lots of other languages and it's very clear what it means, whereas {} are generally used for coding blocks and look terrible for readability.
PHP is similar to languages like C, Java, and Javascript, which all use [] for array identifiers.
If you're saying that $string is not an array, then you can't access a single character using [] or {} and should use substring, but strings clearly are arrays of characters in which case [] is the better option.[/i][/b]
Posted: Thu Nov 25, 2004 12:44 am
by nigma
jason wrote:Basically: $string is NOT an array.
After reading this I went over to the php manual to see how they define strings and arrays. Although I read their definitions I'm having trouble processing them

Could you help me out and tell me what the difference between the two is?
Posted: Sat Nov 27, 2004 8:21 am
by fl0w
Actually, If you look at a c / c++ code, you'll find out that a string is infact a bunch of chars hooked together.
char string[buffersize];
Posted: Sat Nov 27, 2004 8:54 am
by timvw
it becomes a pita if you assume that one char = one byte, imho substr (or mb_substr) is the way to go.
Posted: Sun Nov 28, 2004 3:48 am
by rehfeld
two people have said substr
you would really use a function to single out just 1 character?
i have to agree, substr makes it very clear what your doing. it just seems kinda excessive, to use a function for such a simple thing (unless you need/should be using mb_substr of course)
but then again i guess my question about readability, not efficiency or minimizing code.
thanks for the replies so far, i value all your opinions
Posted: Sun Nov 28, 2004 6:55 am
by Weirdan
PHP Manual wrote:
String access and modification by character
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string in curly braces.
Note: For backwards compatibility, you can still use array-brackets for the same purpose.
However, this syntax is deprecated as of PHP 4.
Posted: Wed Dec 01, 2004 1:08 pm
by BDKR
I agree with you about two. I've made that mistake before.

Posted: Mon Dec 06, 2004 8:01 pm
by fractalvibes
Using a string function certainly makes it clearer what you are dealing with. In most languages a string is simply an array of char....if the PHP manual says $mystring{2} is the preferred method of using a reference modification to refer to a char in that string, by all means do!
fv
Posted: Tue Dec 07, 2004 7:04 am
by Maugrim_The_Reaper
Although $string[] does seem to make more sense, I can see where PHP are coming from. How many newcomers to PHP actually looked at another language? In their cases the more common array version would be confusing. It's probably not the most obvious of methods afterall to someone new to programming in PHP...
Posted: Mon Dec 27, 2004 3:02 am
by carlmcdade
Just my two cents but I tend to think of a string of characters as a stack, where you can only add and remove from one end. This is because of the way c and PHP work.
I only think of a character string as an array if I take the stack and load it into an array.
Posted: Mon Dec 27, 2004 8:01 am
by timvw
i really don't see the link:
- stack that has shift - unshift and pop - push to access elements at the beginning - ending
- array that allows you to access every element through their index.
Posted: Mon Dec 27, 2004 1:20 pm
by carlmcdade
Exactly. A string of characters or bytes is not indexed like an array and can only be manipulated or accessed by breaking off pieces like a stack.