Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using undefined variable causes it to be "conditionally defined" #369

Open
quasilyte opened this issue Mar 20, 2020 · 5 comments
Open

Using undefined variable causes it to be "conditionally defined" #369

quasilyte opened this issue Mar 20, 2020 · 5 comments

Comments

@quasilyte
Copy link
Contributor

Code Example

<?php

function f($cond) {
    if ($cond) {
        echo $x; // 1
    }
    echo $x; // 2
}

Actual Behavior

<critical> ERROR   undefined: Undefined variable: x at hello.php:5
        echo $x; // 1
             ^^
<critical> INFO    undefined: Variable might have not been defined: x at hello.php:7
    echo $x; // 2
         ^^

Expected Behavior

Variable should be reported as undefined twice.

@YuriyNasretdinov
Copy link
Contributor

In PHP the mere act of accessing an undefined variable is defining that variable. So I would say the messages are somewhat correct actually.

@quasilyte
Copy link
Contributor Author

quasilyte commented Mar 20, 2020

I checked it with error_reporting(E_ALL):

<?php

error_reporting(E_ALL);

function f($cond) {
    echo $x;
    echo $x;
}

f(true);

This code gives 2 run-time notices:

PHP Notice:  Undefined variable: x in hello.php on line 6
PHP Notice:  Undefined variable: x in hello.php on line 7
$ php -version
PHP 7.2.19-0ubuntu0.18.04.2 (cli)

@YuriyNasretdinov
Copy link
Contributor

Interesting.

@quasilyte
Copy link
Contributor Author

I'm trying to find undefined variables behavior documentation, but can't find anything as of yet.

@YuriyNasretdinov
Copy link
Contributor

The behaviour of undefined variables is, well... Undefined lol.

quasilyte added a commit that referenced this issue Mar 20, 2020
This change attempts to improve handleIf behavior.

We introduce implicit kind of vars to express automatically
defined vars that can be handled in a special way.

andWalker.varsToDelete are removed because we're running
all branches in a separate block contexts. No extra
cleanup is required.

Changed code requires to evaluate if statement condition
separately, so we can get variables defined inside it
into the parent block context.
assignWalker type does that.

Fixes #369
Fixes #363
Fixes #370

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
quasilyte added a commit that referenced this issue Mar 20, 2020
This change attempts to improve handleIf behavior.

We introduce implicit kind of vars to express automatically
defined vars that can be handled in a special way.

andWalker.varsToDelete are removed because we're running
all branches in a separate block contexts. No extra
cleanup is required.

Changed code requires to evaluate if statement condition
separately, so we can get variables defined inside it
into the parent block context.
assignWalker type does that.

Fixes #369
Fixes #363
Fixes #370

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants