I have 2 php files.
index.php:
<?php
$counter = 0;
require_once('temp.php');
temp();
echo $counter;
?>
temp.php:
<?php
function temp() {
tempHelper();
}
function tempHelper() {
$counter++;
}
?>
I want to print 1 not 0.
I tried to set $counter as global variable without success.
What can I do?
Your
tempHelperfunction is incrementing a local$countervariable, not the global one. You have to either pass the variable in by reference through both functions, or use the global variable:Note that dependence on global variables likely indicates a design flaw in your application.