4

you can do numeric index in string like in array.
ex.

$text = "esenihc gnikcuf yloh";
echo $text[0];
echo $text[1];
echo $text[2];
...................
...................
...................



But if you put string in print_r() not same will happen like in array and you cant do count() with string.

I read the documentation and it says. count() return 1 if not an array in the parameter

print_r() if string is in parameter it just prints that string.

this is not the exact word but something like this.


Why both these functions dont treat string same as an array?

So final question is string an array?

1
  • 2
    I know that, and thats not my question. Commented Sep 19, 2012 at 8:27

5 Answers 5

5

Unlike for example C, PHP has an inbuilt string datatype. The string datatype allows you array-like access to the single characters in the string but will always be a string. So if you pass it to a function that accepts the mixeddatatype this function will determine the datatype of the passed argument and treat it that way. That is way print_r() will print it in the way it was programmed to output strings and not like an array.

If you want a function that works does the same as count for arrays have a look at strlen.

If you want you can "turn" your string into an array through str_split.

Sign up to request clarification or add additional context in comments.

Comments

2

A string is an array if you treat it as an array, eg: echo $text[0], but print_r Prints human-readable information about a variable, so it will output that variable.

It's called Type Juggling

$a    = 'car'; // $a is a string
$a[0] = 'b';   // $a is still a string
echo $a;       // bar

To count a string's length use strlen($string) then you can for a for()

2 Comments

So your answer is, string is an array. It just that count() and print_r() didnt work with string like in normal array.
count() works only on arrays types, if you treating as an array like $string[0] it will show up as array. print_r will print that string or array, in this case string. You can use str_split() to make it array
1

no a string is no array

A string is series of characters, where a character is the same as a byte and An array in PHP is actually an ordered map. A map is a type that associates values to keys.

4 Comments

@Joe sorry i dont understand please tell more
the answer above says it array, then you said it is not.
I was surprised that PHP strings are like C strings (i.e. an array of bytes not Unicode characters) hence the inability to store and manipulate Unicode strings.
No, your answer is a perfect quote of the documentation. I thought it might be wrong (I assumed PHP could handle Unicode (hence the question mark)) but I never said it was wrong!
0

simply everything in the sense every variable in PHP is an array.

Comments

0

Maybe too late but:

<?php
    $text = "esenihc gnikcuf yloh";
    $arrText = explode(" ", $text);

    foreach($arrText as $word) {
        echo $word . "<br>";
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.