Why do I get a different output with $key++ and $key+1?
What should I do to refer to the next element in the loop using foreach?
foreach($diff as $key=>$val) {
if(in_array($diff[$key],$common) && in_array($diff[$key+1],$common)) {}
if(in_array($diff[$key],$common) && in_array($diff[$key++],$common)) {}
}
$x++returns the old value of$x(before the increment). For example,$x=3; print $x++;will print “3”. It also modifies$x, so it’s not the best choice unless that’s your intent. (Here, incrementing would be pretty useless, particularly since the modified keys never see the outside of the loop.)++$xwould return the new value of$x. Like$x++, though, it’s semantically wrong for just getting the next number, as it modifies$x.Just stick with
$x + 1.