Global variable not global?

Hello, the exact same following code works in any online php code testing site (example) but not locally on my kirby installation, and I am puzzled:

<? 
	$h = 1;
	function fun(){
		global $h;
		echo '$h:' . $h ;
	}
	fun();
?>

The output should be:

$h:1

but it is:

$h:

I triple checked I am copypasting the exact same code.

What am I missing?

If I run php -v on the terminal, I get:

PHP 7.0.18-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.18-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies

Thanks

Not sure where you’ve added this, but chances are, your first declaration of $h is not actually in the global space—rather, its context is that of a Kirby method.

You may want to try Kirby’s internal data silo, c:

c::set('h', 1);

function example () {
  echo c::get('h');
}

This is the recommended way to exchange info globally, in a Kirby application. It’s often used for config, temporary object storage, etc.

I know this isn’t a technically precise answer or solution, but I hope it’s useful!

Hello @AugustMiller, and thanks for your answer.

This is in a home template.

I am attempting a recursive function that walks my pages tree and does some things with the pages, and I needed a variable outside of the function that could be accessed from within.

Although it seems as if the set get procedure is simple, I can’t find many examples of it. Toolkit docs are mostly empty about it.

I assume I need to get the value into a variable in order to manipulate it, so:

$h = c::get('h');

before:

$h++;

then:

c::set('h', $h);

?

Thanks again.

<? should be <?php.....
1 Like

No, that does not make sense.

In your config, you set a value:

c::set('h', 1);

Then you get the value in your template:

echo c::get('h'); // will return 1

You can also define a default in the getter, if h is not set.

c::get('h', 2);

But I wonder if you can’t pass $h as a parameter to your function instead of using a global variable?

<?php
$h = 1;
function fun($h){
  echo '$h:' . $h ;
}

fun($h);

In such situations I use something like:


<?php
$h = 1;
function fun() use ($h) {
  echo '$h:' . $h ;
}

fun();

Interesting, @anon77445132, have you really ever tried that?

Hint: You should, really, because you’d then know that this is not possible.

Hello @texnixe,

not here but in plugins and routes.

HeinerEF

[Added:]
Yes, you are right, as always.
The use of “use( … )” is ok for “anonymous functions” like http://php.net/manual/en/functions.anonymous.php says.

Sorry for my bad.

That was a rhetorical question, because you can’t use use in this context. Please read the PHP manual regarding the use of the use keyword.

Using global variables in something simple like a home page template? I think this is one of those scenario’s where you shouldn’t be wondering if you can, but if you should…

There is probably a better way to achieve what you’re trying to do!

1 Like

Thank you all for your comments.

I heard you and rewrote the thing so $h is passed as a variable to the function, recursively.

That way the var is available within the function and keeps its value over recursions.

Something like:

function fun($h){
  if($h < 10){
    $h++;
    fun($h);
  } ...

Thank you!