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

empty function problem in zephir?

there's another question is as follows that about 'empty' function in zephir. The code is:

public function testEmpty()->void
{
    var someVar;
    let someVar = 0;
    echo typeof(someVar) . "\n";
    if empty someVar {
        echo "is empty!";
    }
}

When I use 'zephir build' command, it appears this:

[[email protected] utils]# zephir build
Zephir\CompilerException: Only dynamic/string variables can be used in 'empty' operators in /root/utils/utils/filter.zep on line 23

    if empty someVar {

-------------------------^

Then, I modify code as follows:

public function testEmpty()->void
{
    // var someVar;
    // let someVar = 0;
    // echo typeof(someVar) . "\n";
    // if empty someVar {
    //     echo "is empty!";
    // }

    var someVar;
    let someVar = "";
    echo "\n" . typeof(someVar) . "\n";
    let someVar = 0;
    echo typeof(someVar) . "\n";
    if empty someVar {
        echo "is empty!";
    }
}

buid again:

[[email protected] utils]# zephir build Compiling... Installing... Extension installed! Don't forget to restart your web server

This time is OK. so the test result as follows:

[[email protected] utils]# php example/test.php 
string
integer
is empty!

So, my question is why first condition code build failure? In the second condition just assign string to varablie beofore assign 0 to it, Thanks.



85.5k

create your own method instead, zephir is not php

something like this should work

/**
* returns if value is empty or 0
*
* @param $value
*
* @return bool
*/
public static function isEmpty(value) {

    return (value === null) || (value === '') || (value === 0) || (is_array(value) && count(value) === 0);
}


1.5k

So I think 'empty' function may be have a problem, because I in the first condition, it appears 'Zephir\CompilerException: Only dynamic/string variables can be used in 'empty' operators',please notice that 'Only dynamic/string variables can be used',so I defined a varaible by 'var' that means this's a dynamic variable.So Why not can pass complilation? If not, I think the hint is not exactly correct or may be 'empty' function have a problem for this conditon?

create your own method instead, zephir is not php

something like this should work

/**
* returns if value is empty or 0
*
* @param $value
*
* @return bool
*/
public static function isEmpty(value) {

  return (value === null) || (value === '') || (value === 0) || (is_array(value) && count(value) === 0);
}


85.5k

no idea really, but C language has its own "things", stuff do no magically happen like in php. Everything is 100% strict there



1.5k

Thanks, but i can't understand why the second condition code can pass build, if the first can't, I think the second shouldn't pass compilation.So I doubt here.

no idea really, but C language has its own "things", stuff do no magically happen like in php. Everything is 100% strict there