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 Empty Problem

I learn zephir with documentation, I meet trouble with 'empty' function. The problem as follows:

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

When I use 'zephir build', it appears:
Zephir\CompilerException: Cannot mutate variable 'someVar' because it wasn't defined in /root/utils/utils/filter.zep on line 20

        let someVar = 0;

But when I modify my code as follows:

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

Then use 'zephir build' command, it becames ok as follows:

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

So, Why appear this problem? Anyone can help me? Thank you!



85.5k
Accepted
answer

in C you cant have variabled that you havent defined. so at the begining of every function you var your stuff and after that as you already know you change it with let



1.5k

Thanks, I now undertand the keyword 'let' in zephir diffrenet with ES6's, it can't define a varabile. So 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.

in C you cant have variabled that you havent defined. so at the begining of every function you var your stuff and after that as you already know you change it with let