Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Funkcja count
rudald
post 15.07.2012, 14:41:22
Post #1





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 10.12.2008

Ostrzeżenie: (0%)
-----


1. Czy ktoś ma może dostęp do kodu funkcji "count" lub mógłby mi wytłumaczyć gdzie ją znaleźć?

2. W c++ ostatnim znakiem jest '\0' i wtedy następuje koniec tablicy, czy owy znak istnieje w php jeśli tak to w jakiej postaci?
Go to the top of the page
+Quote Post
redeemer
post 15.07.2012, 15:45:41
Post #2





Grupa: Zarejestrowani
Postów: 915
Pomógł: 210
Dołączył: 8.09.2009
Skąd: Tomaszów Lubelski/Wrocław

Ostrzeżenie: (0%)
-----


Mylisz pojęcia. W C/C++ (chociaż to głównie w C, bo w C++ jest obiekt string) jeżeli mieliśmy tablicę znakową (tzw. "string") to była ona kończona \0, żeby takie funkcje jak printf, strlen itd. wiedziały gdzie kończy się łanćuch.

Jeżeli chcesz policzyć długość łańcucha w PHP - funkcja strlen (dla UTF8 mb_strlen), policzyć ilość elementów tablicy - funkcja count.

Bebechy funkcji count:
Kod
/* {{{ proto int count(mixed var [, int mode])
   Count the number of elements in a variable (usually an array) */
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;
    }
}
/* }}} */

Znajduje się ona w /ext/standard/array.c

PS. Jak chcesz wyszukać implementację jakiejś funkcji w źródłach PHP wyszukuj tekstu PHP_FUNCTION(nazwafunkcji).

Ten post edytował redeemer 15.07.2012, 15:46:53


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 15:50