Jump to content

Understanding Logic


steadythecourse

Recommended Posts

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();?>

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.