# Variable is out of scope

**URL:** <https://www.phphelp.com/t/variable-is-out-of-scope/36626>\
**Category:** General PHP Help\
**Created:** [October 17, 2025, 11:23pm UTC](https://www.phphelp.com/t/variable-is-out-of-scope/36626 "2025-10-17T23:23:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdlvj130](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/a9a28c/32.png) [@cdlvj130](https://www.phphelp.com/u/cdlvj130)\
**Post date:** [October 17, 2025, 11:23pm UTC](https://www.phphelp.com/t/variable-is-out-of-scope/36626/1 "2025-10-17T23:23:51Z")

</div>

why is my code all catawampus?

dbopen return the PDO object  
which passed to dbupdate and is undefined at that point, WHY?  
`
include(“db.php”);
include(“config.php”);
$pdo="";
if ($_SESSION[“onlinestat”] == “0”)
{
$pdo = dbopen();
$_SESSION[“onlinestat”] =“6”;
}
else
{`

`
```
dbupdate($pdo);
$fname=$lname=$address=$city=$state=$zipcode=$phone=$email="";
$status = $_SESSION["onlinemessage"];

```
}
`

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [October 19, 2025, 3:43pm UTC](https://www.phphelp.com/t/variable-is-out-of-scope/36626/2 "2025-10-19T15:43:22Z")

</div>

This is not a scope problem, it is a conditional execution problem. Depending on the result of the conditional comparison, either the code inside the if(){…} block is executed or the code inside the else{…} block is executed, not both. The connection code needs to be unconditionally executed at a common point in the code before it is needed. I recommend that the db.php code make the connection without needing any additional statements.

You should use ‘require’ for things your code must have, Include/require are not functions. The () around the path/filename do nothing and should be removed.
