Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple class to illustrate the inaccuracy of an answer on StackOverflow which has currently been upvoted by at least 23 "programmers" without a single one noticing this incorrect statement.
/**
* Just a simple demonstration after seeing an answer on StackOverflow relating to code auditing which despite
* gathering the most votes by far (currently 23) includes the following clearly inaccurate statement without
* a single comment pointing out the inaccurate claim.
*
* ========================================================================================================
* Quote from the current top answer on http://stackoverflow.com/questions/4273244/auditing-a-php-codebase:
* ========================================================================================================
* Accidental Assignment — More often than not, you'll see this happen in some non-critical component of the
* code, where it can sleep and/or lurk until you get an unexpected result one day: if ($foo = $this->bar()).
* This always evaluates true, of course. In rare circumstances, you'll actually want to test for assignment.
* If that is the case you should use double parenthesis to indicate intent: if (($foo = $this->bar()))
* ========================================================================================================
*
* "Always evaluates true, of course" is the part I have most issue with.
*/
<?php
class BsTester{
function bar(){
return false;
}
function test(){
if($foo = $this->bar()){
echo 'Evaluates TRUE';
}else{
echo 'Evaluates FALSE';
}
}
}
$bsTester = new BsTester();
$bsTester->test(); // Outputs "Evaluates FALSE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.