Static variable in function returning incorrect value...
Posted: Sat Dec 11, 2004 8:58 pm
Okay, this is a bit of a complicated one, so bear with me. Probably be best to explain how it works first.
The script works by having the designer use tags like <=path[forum]> for calling variables and whatnot, and the script going through the code at the end, recognising the custom tags through regexp and getting the right value.
This also includes functions - the code is <#func(args)> where func is the function name and args is the arguments. It does work - I've been able to call predefined and user-created functions with it and they work as they should.
Now, I've got a function that uses a static variable $vinc to go back and forth between 1 and 2 every time the function is called (to do alternating row colours in tables and whatnot). When it's used (the code is <#varinc()>) it goes through the regexp function and the function-calling function, but it returns the same value all the time. For example, if $vinc is set to 1 in the first run, it is always returned 1, despite the fact that $vinc is being set to 2 correctly inside the function. If it's set to, say, 63 in the first run, it's always 63.
Anyway, I've tested it by printing the value of $vinc every time it's called, and I get a regular 1-2-1-2-1 result, but when it's returned a line later, it's 1-1-1-1-1. Tried switching to global, same result. However, when I call the function in PHP, it works.
Here's the applicable code. I hope someone here can help because I'm tearing my hair out figuratively here
Update: Tried using modular division, same result. Also tried putting the static variable earlier in the system and passing it through to varinc() but same result again.
The script works by having the designer use tags like <=path[forum]> for calling variables and whatnot, and the script going through the code at the end, recognising the custom tags through regexp and getting the right value.
This also includes functions - the code is <#func(args)> where func is the function name and args is the arguments. It does work - I've been able to call predefined and user-created functions with it and they work as they should.
Now, I've got a function that uses a static variable $vinc to go back and forth between 1 and 2 every time the function is called (to do alternating row colours in tables and whatnot). When it's used (the code is <#varinc()>) it goes through the regexp function and the function-calling function, but it returns the same value all the time. For example, if $vinc is set to 1 in the first run, it is always returned 1, despite the fact that $vinc is being set to 2 correctly inside the function. If it's set to, say, 63 in the first run, it's always 63.
Anyway, I've tested it by printing the value of $vinc every time it's called, and I get a regular 1-2-1-2-1 result, but when it's returned a line later, it's 1-1-1-1-1. Tried switching to global, same result. However, when I call the function in PHP, it works.
Here's the applicable code. I hope someone here can help because I'm tearing my hair out figuratively here
Code: Select all
<?php
// scan_page(STRING html) - scans html for email addresses, large images, and var/array/function references, and returns fixed code
function scan_page($html) {
// only showing function part - the rest are practically the same anyway ;p
preg_match_all("/<#([_a-z0-9]+)\((.*)\)>/i", $html, $temp_func);
for ($i = 0; $temp_func[1][$i]; $i++) {
$find_func[$i] = $temp_func[0][$i];
$replace_func[$i] = get_func($temp_func[1][$i], $temp_func[2][$i]);
}
$html = str_replace($find_func, $replace_func, $html);
return $html;
}
// get_func(STRING function, STRING values) - returns the value of the function specified by function
function get_func($func, $values) {
$values = explode(',', $values);
return call_user_func_array($func, $values);
}
// varinc(NULL) - returns an alternating number between 1 and 2
function varinc() {
static $vinc;
if ($vinc != 1) $vinc = 1;
else $vinc = 2;
print $vinc .':'; // my happy-happy test ¬_¬
return $vinc;
}
// $index_content has all the HTML and tags at this point
$index_content = scan_page($index_content);
// output
print $index_content;
?>