steadythecourse Posted October 5, 2010 Share Posted October 5, 2010 Hi All, I'm trying to understand the logic behind this bit of code I found. I (think I) get that it takes an argument which in this case is a number, an checks to see if the array isset, then uses a while loop to loop through the possible values taking each value and placing it into another function which performs some task. What I'm really confused about is how the code is using unset, If I'm correct it's unsetting the incremented value which is the value that should be used in the next iteration, now I know this is not the case, but I don't understand why this is working. I have included the code in the topic and I also wrote a small piece of code to mirror this so I could play around with it in hopes of understanding the logic but it gives me a syntax error in my IDE when I try to unset the incremented value. I know this is probably something really stupid so be kind. <?phpdefine("NUM_0", 0);define("NUM_1", 1);define("NUM_2", 2);define("NUM_3", 3);define("NUM_4", 4);function function_1($position) { static $positions = array(NUM_0,NUM_1,NUM_2,NUM_3,NUM_4,), $index = 0; if (isset($positions[$index])) { while ($position >= $index) { $current_position = $positions[$index]; unset($positions[$index++]); function_2($current_position); } }}function function_2($position) { switch ($position) { case 0: echo '$position equals 0 <br />'; break; case 1: echo '$position equals 1 <br />'; break; case 2: echo '$position equals 2 <br />'; break; case 3: echo '$position equals 3 <br />'; break; case 4: echo '$position equals 4 <br />'; break; }}function_1(NUM_4);[b][u]output![/u]$position equals 0$position equals 1$position equals 2$position equals 3$position equals 4 [/b]function test() { static $var_1; $var_1 = 10; if (isset($var_1)) { while ($var_1 >= 0) { $var_2 = $var_1; unset ($var_1++); // syntax error on this line. echo $var_2; } }}test();?> Quote Link to comment https://forums.phpfreaks.com/topic/215220-understanding-logic/ Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2010 Share Posted October 5, 2010 if you want to unset $var_1, don't add the ++ operator on the end of it. Quote Link to comment https://forums.phpfreaks.com/topic/215220-understanding-logic/#findComment-1119362 Share on other sites More sharing options...
Psycho Posted October 5, 2010 Share Posted October 5, 2010 I have included the code in the topic and I also wrote a small piece of code to mirror this ... I don't see the original code, but I see two uses of unset in your example unset($positions[$index++]); and unset ($var_1++); The second usage makes no sense. Why would you want to unset a variable while at the same time incrementing it. However, the first example does make sense. In that example you are unsetting the array variable $positions[n] where n is the value of $index. Then $index is incremented by 1. It is not trying to unset $index - only the array value with an index equal to the $index variable. Quote Link to comment https://forums.phpfreaks.com/topic/215220-understanding-logic/#findComment-1119366 Share on other sites More sharing options...
steadythecourse Posted October 5, 2010 Author Share Posted October 5, 2010 thanks mjdamato, I got it! steadythecourse Quote Link to comment https://forums.phpfreaks.com/topic/215220-understanding-logic/#findComment-1119404 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.