It depends. Many PHP files in a Kirby installation (for sure all templates, snippets, controllers, config, and probably others) do not execute in global scope. Templates for example, very simplified, are run like this by Kirby:
function render($template) {
$page = page();
$site = site();
// other kirby variables
include $template;
}
render("site/templates/default.php");
The variables you define in “default.php” therefore are executed in the scope of the render function and are not by default global. The named (as in “not anonymous”) functions that you define in “default.php” however get hoisted (stealing from JS nomenclature here for lack of a better word) into global space by PHP.
Therefore, if your example is a template, the situation actually looks more like this:
function render() {
// ... kirby stuff
// your stuff:
$pagecount = 1;
$data = null;
$array = null;
$submissions = array();
$meta = null;
$total = null;
$totalcount = 0;
$token = "bla";
function hoppla() {
global $data, $array, $submissions, $meta, $total, $totalcount, $pagecount, $token;
… rest of code
}
hoppla();
}
render();
After “hoisting”, that would look like this:
function render() {
// ... kirby stuff
// your stuff:
$pagecount = 1;
$data = null;
$array = null;
$submissions = array();
$meta = null;
$total = null;
$totalcount = 0;
$token = "bla";
hoppla();
}
function hoppla() {
global $data, $array, $submissions, $meta, $total, $totalcount, $pagecount, $token;
… rest of code
}
render();
and that’s why it doesn’t work.