Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
<?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
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.
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.
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]
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?
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
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!
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...
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.