How to Implement a Custom str_concat Function in a PHP Extension

This article demonstrates how to create a PHP extension that adds a str_concat function, explaining the underlying zend_string structure, required macros, and providing complete source code to handle prefix detection and string concatenation within the extension.

21CTO
21CTO
21CTO
How to Implement a Custom str_concat Function in a PHP Extension

Introduction

The PHP string handling functions are powerful, and their implementations are written in C within the Zend engine. This article shows how to process strings inside a PHP extension by adding a custom str_concat function.

Basic PHP Implementation

<?php
function str_concat($prefix, $string) {
    $len = strlen($prefix);
    $substr = substr($string, 0, $len);
    if ($substr != $prefix) {
        return $prefix . " " . $string;
    } else {
        return $string;
    }
}

echo str_concat("hello", "word");
echo "
";

echo str_concat("hello", "hello bo56.com");
echo "
";
?>

This function concatenates the prefix and the original string only when the original string does not already start with the given prefix.

PHP Extension Implementation

PHP_FUNCTION(str_concat)
{
    zend_string *prefix, *subject, *result;
    zval *string;
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &prefix, &string) == FAILURE) {
        return;
    }
    subject = zval_get_string(string);
    if (zend_binary_strncmp(ZSTR_VAL(prefix), ZSTR_LEN(prefix), ZSTR_VAL(subject), ZSTR_LEN(subject), ZSTR_LEN(prefix)) == 0) {
        RETURN_STR(subject);
    }
    result = strpprintf(0, "%s %s", ZSTR_VAL(prefix), ZSTR_VAL(subject));
    RETURN_STR(result);
}

The extension registers a new function str_concat. It parses a zend_string prefix and a zval string, checks whether the subject already starts with the prefix using zend_binary_strncmp, and either returns the original string or concatenates the prefix and subject.

zend_string Structure

struct _zend_string {
    zend_refcounted_h gc;   /* GC information */
    zend_ulong h;           /* hash value */
    size_t len;             /* string length */
    char val[1];            /* start of the string data */
};
zend_string

is a new structure introduced in PHP 7 to represent immutable strings efficiently.

Zend Macros

#define ZSTR_VAL(zstr)  (zstr)->val
#define ZSTR_LEN(zstr)  (zstr)->len
#define ZSTR_H(zstr)    (zstr)->h
#define ZSTR_HASH(zstr) zend_string_hash_val(zstr)

These macros provide direct access to the members of zend_string. They are defined in Zend/zend_string.h. Additional string‑related functions such as zval_get_string and zend_binary_strncmp are declared in Zend/zend_operators.h. For more macros, refer to Zend/zend_API.h.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPExtension DevelopmentString ManipulationZend engine
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.