justinh Posted December 10, 2008 Share Posted December 10, 2008 Hi, I'm currently learning about PHP functions. I wrote this function out: <?php function myfuction(){ static $a = 0; ++$a; if($a = 1){ echo "this function has been loaded " .$a. " time<br />"; } else { echo "this function has been loaded " .$a ." time(s)<br />"; } } for($i = 0; $i <= 8; ++$i){ myfuction(); } ?> this returns this function has been loaded 1 time this function has been loaded 1 time this function has been loaded 1 time this function has been loaded 1 time this function has been loaded 1 time this function has been loaded 1 time this function has been loaded 1 time this function has been loaded 1 time this function has been loaded 1 time My goal was to make it where the function checks if $a = 1 and if it does.. makes the line singular. Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/ Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 if($a == 1){ echo "this function has been loaded " .$a. " time<br />"; } else { echo "this function has been loaded " .$a ." time(s)<br />"; } = sets the value to 1 which returns true, == checks if the value is equaled to one. Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711496 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 you are resetting the number to 0 every time on line one of your function i think though i'm not familiar with "static". <?php function myfuction(){ if(!isset($a)){ $a = 0; } ++$a; if($a == 1){ echo "this function has been loaded " .$a. " time<br />"; } else { echo "this function has been loaded " .$a ." time(s)<br />"; } } for($i = 0; $i <= 8; ++$i){ myfuction(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711500 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 you are resetting the number to 0 every time on line one of your function i think though i'm not familiar with "static". <?php function myfuction(){ if(!isset($a)){ $a = 0; } ++$a; if($a == 1){ echo "this function has been loaded " .$a. " time<br />"; } else { echo "this function has been loaded " .$a ." time(s)<br />"; } } for($i = 0; $i <= 8; ++$i){ myfuction(); } ?> Brian brings up a great point, Function variables are local to that function. After looking up static This function is quite useless since every time it is called it sets $a to 0 and prints "0". The $a++ which increments the variable serves no purpose since as soon as the function exits the $a variable disappears. To make a useful counting function which will not lose track of the current count, the $a variable is declared static: Example #5 Example use of static variables <?php function test() { static $a = 0; echo $a; $a++; } ?> Now, $a is initialized only in first call of function and every time the test() function is called it will print the value of $a and increment it. Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable $count to know when to stop: Example #6 Static variables with recursive functions <?php function test() { static $count = 0; $count++; echo $count; if ($count < 10) { test(); } $count--; } ?> You need to make the function recursive for it to work. If you do not want to make it recursive, this should work. <?php function myfuction(){ global $a; ++$a; if($a == 1){ echo "this function has been loaded " .$a. " time<br />"; } else { echo "this function has been loaded " .$a ." time(s)<br />"; } } $a=0; for($i = 0; $i <= 8; ++$i){ myfuction(); } ?> That should get you the desired result. Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711504 Share on other sites More sharing options...
Maq Posted December 10, 2008 Share Posted December 10, 2008 They are right, you were assigning (=) a to 1 every time you looped through. You need to compare (==) a to 1. Why not pass $i to the function in the for loop? function myfuction($a){ $a+=1; echo ($a==1) ? "this function has been loaded " .$a. "time " : "this function has been loaded " .$a ." time(s) " } for($i = 0; $i myfuction($i); } ?> FYI: You spelled function wrong. Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711509 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 thank you for enlightening me/us to what "static" is, premiso. So that line is fine in his code, he just needs to change = to ==, right? Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711512 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 thank you for enlightening me/us to what "static" is, premiso. So that line is fine in his code, he just needs to change = to ==, right? Yea, pretty much. Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711513 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 premiso: If you use global inside the function, variable's scope will extend outside the function. The whole point of static variables is that they are 'remembered' after function is executed, but retain the function scope. In addition, recursion is when something calls itself, which is not what you're doing in your solution. justinh: Just thought I'd point out that grammatically, putting (s) at the end of something makes it optional, so technically, you would have time(s) no matter how many times the function is called. In your if..else condition, you would want to display 'time' or 'times'; no parenthesis. Just a minor bitch from the grammar police. <?php function myfuction(){ static $a = 1; if ($a == 1) { echo "this function has been loaded " .$a. " time<br />"; } else { echo "this function has been loaded " .$a. " times<br />"; } $a++; } for($i = 0; $i <= 8; ++$i){ myfuction(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711517 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 premiso: If you use global inside the function, variable's scope will extend outside the function. The whole point of static variables is that they are 'remembered' after function is executed, but retain the function scope. In addition, recursion is when something calls itself, which is not what you're doing in your solution. Yea, I saw my mistake after the time ran out to edit. But I did not offer up a recursive solution, " If you do not want to make it recursive, this should work." Either way that post is very misleading, as he had the static usage right and I mis-read the php page thinking that it was referring to the lower example as appose to the upper one when it said this would not work. But yea, thanks for pointing it out to him. Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711521 Share on other sites More sharing options...
justinh Posted December 10, 2008 Author Share Posted December 10, 2008 Yeah sorry about the grammar. I first had it only returning time(s).. then decided to try the if statement. Didn't bother changing it. =/ Quote Link to comment https://forums.phpfreaks.com/topic/136373-function-problem/#findComment-711561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.