Hi,
I have a question regarding php defined function. I want to see the logic implemented for all the php defined functions. How can I see that. For example I want to see what is the logic behing strstr or how strtolower works.Where can I see in wamp these definitions.
Thanks in advance.

Recommended Answers

All 2 Replies

Hi,
I have a question regarding php defined function. I want to see the logic implemented for all the php defined functions. How can I see that. For example I want to see what is the logic behing strstr or how strtolower works.Where can I see in wamp these definitions.
Thanks in advance.

PHP is an open source project. If you want to, you can download the source and look at how those functions are defined. Or you can browse through it via PHP's Github account, found here.

The strstr() function, for example, can be found in ext/standard/string.c:

/* {{{ proto string strstr(string haystack, string needle[, bool part])
   Finds first occurrence of a string within another */
PHP_FUNCTION(strstr)
{
    zval *needle;
    char *haystack;
    int haystack_len;
    char *found = NULL;
    char needle_char[2];
    long found_offset;
    zend_bool part = 0;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|b", &haystack, &haystack_len, &needle, &part) == FAILURE) {
        return;
    }

    if (Z_TYPE_P(needle) == IS_STRING) {
        if (!Z_STRLEN_P(needle)) {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");
            RETURN_FALSE;
        }

        found = php_memnstr(haystack, Z_STRVAL_P(needle), Z_STRLEN_P(needle), haystack + haystack_len);
    } else {
        if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
            RETURN_FALSE;
        }
        needle_char[1] = 0;

        found = php_memnstr(haystack, needle_char,  1, haystack + haystack_len);
    }

    if (found) {
        found_offset = found - haystack;
        if (part) {
            RETURN_STRINGL(haystack, found_offset, 1);
        } else {
            RETURN_STRINGL(found, haystack_len - found_offset, 1);
        }
    }
    RETURN_FALSE;
}

I always use PHP as an OOP language (it is a cheap language in terms of system recourses and employs salaries) . Sometimes I forget what amateur code lye underneath the sophisticated layers we have to create. A big thanks Bob for remind me this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.