waslsdnowlds Posted October 16, 2017 Share Posted October 16, 2017 I don't completely understand this code. I'm trying to grasp my mind around it. I get what it returns (7) but I don't understand that if($$v++) part. $$v++ I'm guessing compares the current $v to the next value in the array creating a new variable $$v? Is that right? function fdup($a){ foreach($a as $v) if($$v++) return $v; return -1; } $a = array(10, 6, 7, 4, 9, 1, 7, 2, 5, 3); fdup($a); Quote Link to comment https://forums.phpfreaks.com/topic/305373-understanding-and-foreach/ Share on other sites More sharing options...
requinix Posted October 16, 2017 Share Posted October 16, 2017 So we're clear: that is terrible code and should never be used for anything except asking "oh wow, WTF does this do?" $$ is part of "variable variables", a powerful but oh so often misused feature of PHP. $foo = "bar"; $bar = 123; echo $foo; // bar echo $$foo; // 123That is, "$$variable" means "find a variable whose name is the current value of $variable". Try stepping through the code: 1. $v is 10. That means $$v will be $10 (if that were a valid variable name), which is undefined. $$v++ will create a variable $10, "return" 0, and increment it, so $$v++ is 0 and $$v = $10 = 1. 2. $v is 6. Same thing, $$v = $6 = undefined, $$v++ is 0 and $6 = 1. 3. $v is 7, $$v = $7 = undefined, $$v++ is 0 and $7 = 1. 4-6. Same for $4, $9, and $1. 7. $v is 7 again. $$v = $7 = 1 from earlier, $$v++ is 1 (and $7 = 2) so the function returns $v (7). Let's rewrite the code in a more reasonable way: function fdup($a) { // start with an empty array. mimics how none of the $$v variables existed beforehand $found = []; foreach ($a as $v) { // same post-increment but on an entry in $found instead of a variable // PHP will actually warn you when $found[$v] does not yet exist so this isn't best practice if ($found[$v]++) { return $v; } } return -1; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/305373-understanding-and-foreach/#findComment-1552738 Share on other sites More sharing options...
gizmola Posted October 16, 2017 Share Posted October 16, 2017 I understand why this is hard to learn about, because you aren't able to easily find a reference to '$$' in the php manual. You have to know that '$$' is an implementation of Variable variables. The other thing you were clearly confused by (and the code is meant to illustrate order of operations and boolean typecasting as well) is the way the postfix increment operator (++) works. People are often familiar with the use of the operator in for loops, but in this bit of code, the function depends on it. $v = 0; if ($v++) { echo 'True'; } else { echo 'False'; } if ($v) { echo "\nTrue now"; } Hopefully it's clear that evaluation of the boolean truthiness of the variable occurs BEFORE the variable is incremented. Quote Link to comment https://forums.phpfreaks.com/topic/305373-understanding-and-foreach/#findComment-1552740 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.