02-22-2008, 07:54 PM
|
#4 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 154
Thanks: 31
|
You can think about this two ways.
First, you can access the string as if it were an array (which you're attempting to do), e.g.
PHP Code:
$string= 'hello';
// display the first character in the string, at index 0
echo $string[0]; // 'h'
In your loop, you would refer to it with the [ ] brackets, not the { } braces. I haven't tested it, but I believe that actually tacks the numeric value of $i to the end of $string at that point in the loop. The other important factor, $char is not defined as an array. It's just a string as well, which is reassigned each time through the loop. I think you want something more like this
PHP Code:
// our string
$string= 'hello';
// an empty array
$charArray= array();
for ( $i=0; $i< strlen($string); $i++ ) {
// assign each character in the string to an empty index
$charArray[]= $string[$i];
}
// display the array values
print_r($charArray);
__________________
I reject your reality, and substitute my own.
|
|
|
|