Search code examples
phpcphp-extension

evaluate php code in php extension


I am writing a php extension( i am newbie) and now i encounter a weird problem:

Is there any way to evaluate php code in string INSIDE a php extension ?

for example bellow if zname would be "echo 'hello';" it will print hello not echo 'hello';

thanks in advance

PHP_FUNCTION(hello_greetme)
{
    zval *zname;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zname) == FAILURE) {
        RETURN_NULL();
    }

    convert_to_string(zname);
    php_printf("Hello ");
    PHPWRITE(Z_STRVAL_P(zname), Z_STRLEN_P(zname));
    php_printf("\n");


    RETURN_TRUE;
}

Solution

  • int eval_php_code(char *code) {
        int ret = 0;
    
        zend_try {
            ret = zend_eval_string(code, NULL, (char *)"" TSRMLS_CC);
        } zend_catch {
    
        } zend_end_try();
    
        return ret == FAILURE;
    }
    

    This should work for you.