Passing parameters by reference in PHP extension

c Code:

PHPAPI zval *php_array_set(zval *arr, char *p1, size_t len, const zval *val)
{
    zval *tmpVal;

    tmpVal = zend_hash_str_find(Z_ARRVAL_P(arr), p1, len);
    if (tmpVal != NULL) {
        ZVAL_DEREF(tmpVal);
        ZVAL_COPY(tmpVal, val);
    }

    return arr;
}

PHP_FUNCTION(array_set) /* {{{ */
{
    zval *arr, *val;
    char *key;
    size_t len;

    ZEND_PARSE_PARAMETERS_START(3, 3)
        Z_PARAM_ARRAY_EX(arr, 0, 1);
        Z_PARAM_STRING(key, len);
        Z_PARAM_ZVAL(val);
    ZEND_PARSE_PARAMETERS_END();

    php_array_set(arr, key, len, val);
    
    ZVAL_COPY(return_value, arr);
}

php Code:

$arr1 = [
    "a" => 1111
];

$arr2 = array_set($arr1, "a", 2222);
var_dump($arr1, $arr2);

array_set receives a reference parameter and modifies the value corresponding to the key value.
when debugging with gdb, the arr parameter is printed after calling the php_array_set method, and

is modified successfully.

but it turns out that $arr2 was modified successfully, but $arr1 is still the original value. Excuse me, what is the reason for this?

Php
Apr.09,2021


ZEND_BEGIN_ARG_INFO_EX(arginfo_krsort, 0, 0, 1)
    ZEND_ARG_INFO(1, arg) /* ARRAY_INFO(1, arg, 0) */
    ZEND_ARG_INFO(0, sort_flags)
ZEND_END_ARG_INFO()

-sharpdefine ZEND_ARG_INFO(pass_by_ref, name)
Menu