Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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!



67.7k
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



636

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