Ok, PHP is not in my major stack and haven't really gotten deep into it, I just want to get things done so I'll go straight to the code:
<?php require_once('server_helper.php'); ?>
<?php
echo $table_balance; // "balance"
function get_balance($user_id) {
try {
$conn = establish_connection();
echo $table_balance; // ""
// some more code...
} catch (Exception $e) {
print_r($e);
}
}
Explanation: server_helper.php
is one level up and it has $table_balance = 'balance';
in it.
The first echo $table_balance;
- outside the function works as expected and prints "balance"
, while the second one - inside the function prints ""
.
The establish_connection()
function is also from the server_helper.php
and it's working fine...
I'm including the file with the above code in another file, which handles the route hit, gets the ID and calls get_balance($some_id);
The code in get_balance()
used to be in that another file, where I've used to include the server_helper.php
and it worked like a charm. The reason for moving the code is that I wanted to abstract away the DB communication in a separate layer.
I've tried with include
, include_once
, require
and require_once
with no success.
P.S: I know underscore is discouraged but I don't feel comfortable using camelCase in a case-insensitive languages.