I'm using the extension PHP intelephense for Visual Studio Code.

$root = $_SERVER['DOCUMENT_ROOT']; include($root . '/sqlcon.php'); $gettextquery = mysqli_query($con, "SELECT * FROM `pics` WHERE ai_cats = ''");

I get

Undefined variable '$con'

even though $con is defined in sqlcon.php.

Everything worked until recently. I've tried to solve it by clearing cache and restarting.

Your Common Sense's user avatar

TheGreatCornholio's user avatar

1

Intelephense does static analysis — it doesn't actually execute the include, so it can't follow $_SERVER['DOCUMENT_ROOT'] at runtime to find $con.

One quick fix would be to add a stub at the top of your file:

/** @var mysqli $con */

Or check your intelephense.environment.includePaths setting in VS Code and add your root path there so it can resolve the include statically.

Tuhin Shaikh's user avatar

2 Comments

Yeah I've got this from chatGPT too, but I don't want to use that. "it doesn't actually execute the include" but it recognized the defined variables in includes

2026-05-01T20:02:58.373Z+00:00

Please don't post your answer if you cannot write it yourself.

2026-05-01T20:15:32.347Z+00:00

I'm just switching from

$root = $_SERVER['DOCUMENT_ROOT'];

to

_DIR_

now, intelephense recognizes the variables and their types in included files without any problem again with that

TheGreatCornholio's user avatar

2 Comments

Only, it works if this file stays in the same directory, which is not always so.

2026-05-01T21:25:54.87Z+00:00

Also, doesn't explain why it allegedly "worked" before.

2026-05-01T21:31:12.683Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.