Reputation: 7732
I have a script with the following part:
function checkDays($oldDate){
// calculate time
// Print table row style based on number of days passed
// (eg less than 1 week, 1 week, 2 weeks, 3weeks+)
}
//Some code
checkdays($value)
I would like to put a counter of how many records of each time period there is, the function itself is inside a loop, so its called for each row of the table, and I can't seem to access variables defined outside the function itself so I can change them (eg. put a counter at the top of the script and modify it from the function).
Some people say use global variables but I understand that is a risk and not recommended. Is there an easy way to access the variables from the function? Also, is there a better way to do what I'm doing here overall?
Upvotes: 6
Views: 11602
Reputation: 179046
Rather than pollute the global namespace with useless variables, you can define static members of functions, that persist beyond the scope of the function, but only exist within the scope of the function:
function myFunction($param)
{
static $count;
if (empty($count)) $count = 0;
$count++;
//rest of your function
}
Unfortunately, I can't seem to find where this functionality is documented in the php docs.
Found it: variable scope
I can't seem to access variables defined outside the function itself
I had missed this line.
If you're in an OOP paradigm, use an object to store the counter. Otherwise, global variables are the way to go, but not directly. If you're coding in a scripted paradigm, and you're using the latest version of PHP, use namespaces to keep from polluting the root namespace's globals.
If you can't use namespaces, use pseudo-namespaces for everything. Instead of function someFunction
, use function projectName_someFunction
, or some combination thereof. That way you can combine multiple projects without worrying about using the wrong function. In that same manner, all global variables can then be stored as $projectName_someVar
.
There are other methods for pseudo-namespacing your function/variable names.
The reason not to use global variables is that a global variable with a common name can easily be overwritten, and may be ambiguous: what does $count
actually count in the global namespace?
Upvotes: 1
Reputation: 10087
I think the risk you're referring to is with register_globals
, which is something else entirely.
Inside the function, use the global
keyword, e.g.
function myFunction($someParam) {
global $counter;
++$counter;
// do something
}
Alternatively, you can do what's called pass-by-reference to modify a variable in the outer scope that's passed to your function, e.g.
function myFunction($someParam, &$counter) {
++$counter;
// do something
}
Note the ampersand.
More information is available at the PHP Manual page about Variable Scope
Upvotes: 5
Reputation: 10583
DO NOT BE TEMPTED TO USE globals
You can pass variables by reference, this means the actual variable is passed rather than a copy of it:
$count = 0;
//Some code
checkdays($value, $count);
checkdays($value, $count);
checkdays($value, $count);
// This will output 3
echo $count;
// Use a & to pass by reference
function checkDays($oldDate, &$count){
// Your code goes here as normal
// Increment $count, because it was passed by reference the
// actual variable was passed
// into the function rather than a copy of the variable
$count++;
}
Upvotes: 11
Reputation: 6837
You should probably create a class and use private properties for storing the data you want.
Upvotes: 4