0

PHP string act as an array as bellow,

$string='test string'; 
echo $string[0];  // print t

But when I use count function with string variable it prints 1 only.

echo count($string); // print 1

Can anyone explain why is that?

3
  • 1
    Make use of strlen() for a string. Commented Dec 3, 2013 at 12:21
  • why does count($string) print 1 ? Commented Dec 3, 2013 at 12:22
  • 4
    If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. Read the docs: us1.php.net/manual/en/function.count.php Commented Dec 3, 2013 at 12:24

3 Answers 3

8

$string[offset] is only syntactic sugar to let you easily access a specific byte of the string. Even though the syntax is identical to that of accessing array indices, it does not mean that strings act as arrays in any way. count is a function that works on arrays, not strings.

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

When you access the string components as an array the runtime detects what you are doing and picks up the character at the given location. This is in line with what other languages do and is a common shortcut.

When you call "count", what PHP actually does is cast it to an array (unless it implements countable. Try:

var_dump( (array)$string);

See the documentation for count: https://www.php.net/count

If you're looking for an alternative, strlen will give you the length (in bytes), mb_strlen will give you the length in number of characters (not the same in the case of multi-byte character sets)

Comments

0

The reason is.. well let me just link you to the actual implementation of count function: https://github.com/php/php-src/blob/master/ext/standard/array.c (notice that it's included in the logical place: array.c as it's meant for arrays/countable really, not strings).

PHP_FUNCTION(count)
{   
        zval *array;
        long mode = COUNT_NORMAL;

        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &array, &mode) == FAILURE) {
                return;
        }   

        switch (Z_TYPE_P(array)) {
                case IS_NULL:
                        RETURN_LONG(0);
                        break;
                case IS_ARRAY:
                        RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC));
                        break;
                case IS_OBJECT: {
#ifdef HAVE_SPL
                        zval *retval;
#endif
                        /* first, we check if the handler is defined */
                        if (Z_OBJ_HT_P(array)->count_elements) {
                                RETVAL_LONG(1);
                                if (SUCCESS == Z_OBJ_HT(*array)->count_elements(array, &Z_LVAL_P(return_value) TSRMLS_CC)) {
                                        return;
                                }   
                        }   
#ifdef HAVE_SPL
                        /* if not and the object implements Countable we call its count() method */
                        if (Z_OBJ_HT_P(array)->get_class_entry && instanceof_function(Z_OBJCE_P(array), spl_ce_Countable TSRMLS_CC)) {
                                zend_call_method_with_0_params(&array, NULL, NULL, "count", &retval);
                                if (retval) {
                                        convert_to_long_ex(&retval);
                                        RETVAL_LONG(Z_LVAL_P(retval));
                                        zval_ptr_dtor(&retval);
                                }   
                                return;
                        }   
#endif
                }   
                default:
                        RETURN_LONG(1);
                        break;
        }   
}   

As you can see.. the count function is really only implemented for arrays, spl countables, and NULL (return 0). Anything else will return the int value: 1.

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.