We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Zephir Char problem

Hello. I want to operate with ASCII representation of Chars, but code below works wrong:

string str = "test";
char ch; string ret = "";
for ch in str {
    let ret .= chr(ord(ch));
}

It returns "1111". It's very strange.



98.9k

Not sure, what are you expecting there. A character is different than a string.

https://en.wikipedia.org/wiki/Character_(computing) https://en.wikipedia.org/wiki/String_(computer_science)

edited Jul '14

I know the difference between Char and String types in non-PHP languages. But pure PHP doesn't have this type. PHP function ord() expects one-symbol string, so it is very good idea to be compatible with this new type, because one-symbol string is a char...



98.9k

PHP function ord() expects a parameter string but you're passing a char, you may want to add a cast if you want to pass a string:

for ch in str {
    let ret .= chr(ord((string) ch));
}
edited Jul '14

Yes, I tried to cast it to string, but I can't compile with it:

    [...]/myextension/common.zep.c: В функции ‘zim_MyExtension_Common_test’:
    [...]/myextension/common.zep.c:62: ошибка: invalid type argument of ‘unary *’ (have ‘int’)
    [...]/myextension/common.zep.c:62: предупреждение: при передаче аргумента 1 ‘zval_addref_p’ целое преобразуется в указатель без приведения типа
    /usr/include/php/Zend/zend.h:404: замечание: expected ‘struct zval *’ but argument is of type ‘char’
    [...]/myextension/common.zep.c:62: предупреждение: в присваивании целое преобразуется в указатель без приведения типа
    [...]/myextension/common.zep.c:62: предупреждение: при передаче аргумента 1 ‘zephir_make_printable_zval’ целое преобразуется в указатель без приведения типа
    ./kernel/operators.h:83: замечание: expected ‘struct zval *’ but argument is of type ‘char’
    make:  [myextension/common.lo] Ошибка 1

Where C file contains

    int ZEPHIR_LAST_CALL_STATUS;
    zephir_nts_static zephir_fcall_cache_entry *_3 = NULL, *_5 = NULL;
    long _0;
    char ch;
    zval *str_param = NULL, *_2 = NULL, *_4 = NULL;
    zval *str = NULL, *ret, *_1 = NULL;

    ZEPHIR_MM_GROW();
    zephir_fetch_params(1, 1, 0, &str_param);

    if (unlikely(Z_TYPE_P(str_param) != IS_STRING && Z_TYPE_P(str_param) != IS_NULL)) {
        zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'str' must be a string") TSRMLS_CC);
        RETURN_MM_NULL();
    }

    if (unlikely(Z_TYPE_P(str_param) == IS_STRING)) {
        str = str_param;
    } else {
        ZEPHIR_INIT_VAR(str);
        ZVAL_EMPTY_STRING(str);
    }
    ZEPHIR_INIT_VAR(ret);
    ZVAL_STRING(ret, "", 1);

    for (_0 = 0; _0 < Z_STRLEN_P(str); _0++) {
        ch = ZEPHIR_STRING_OFFSET(str, _0);
        zephir_get_strval(_1, ch);
        ZEPHIR_CALL_FUNCTION(&_2, "ord", &_3, _1);
        zephir_check_call_status();
        ZEPHIR_CALL_FUNCTION(&_4, "chr", &_5, _2);
        zephir_check_call_status();
        zephir_concat_self(&ret, _4 TSRMLS_CC);
    }
    RETURN_CTOR(ret);

Line 62 — "zephir_get_strval(_1, ch);"

P.S. Sorry for Russian :)