code readability question

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.

Moderator: General Moderators

rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

code readability question

Post 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?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post 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]
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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?
fl0w
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2004 2:19 pm
Location: Stockholm Sweden

Post 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];
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

it becomes a pita if you assume that one char = one byte, imho substr (or mb_substr) is the way to go.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

I agree with you about two. I've made that mistake before. :oops:
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post 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.
Post Reply