Logic issue in PHP -
ok, tested follows , i'll let know discovered:
echo ('-1' < 0) ? 'true' : 'false'; // echo "true" echo ('1' > 0) ? 'true' : 'false'; // echo "true" # notice '-1' , '1' strings
now let's take array, coming database after filtering result in order rows uid = 1
.
$this->a = array( [0] => array( 'uid' => '1', 'pid' => '91', 'amount' => '-1' ), [1] => array( 'uid' => '1', 'pid' => '92', 'amount' => '1' ), [2] => array( 'uid' => '1', 'pid' => '93', 'amount' => '1' ) );
now want create function posamount($pid)
returns true
if 'amount' > 0
or false
if 'amount' < 0
. (notice: amount = 0 don't care). i'd write similar function called negamount($pid)
returns exactely opposite of first. i'd like, now, introduce twin functions:
public function posamount($pid) { foreach ($this->a $a) { if (count($this->a) == 0) { return false; } return ($a['pid'] == $pid , $a['amount'] > 0) ? true : false; } } public function negamount($pid) { foreach ($this->a $a) { if (count($this->a) == 0) { return false; } return ($a['pid'] == $pid , $a['amount'] < 0) ? true : false; } }
the cool fact that, regarding first array (which, checked var_dump()
keeps nature trough entire script):
$istance->negamount(91); // returns true, expected $istance->posamount(92); // returns false, not expected. # why god wants me mad?
the problem returning on first iteration of foreach loop. should rewrite functions this:
public function negamount($pid) { if (count($this->a) == 0) { return false; } foreach ($this->a $a) { if ($a['pid'] == $pid) { if ($a['amount'] < 0) { return true; } } } return false; } public function posamount($pid) { if (count($this->a) == 0) { return false; } foreach ($this->a $a) { if ($a['pid'] == $pid) { if ($a['amount'] > 0) { return true; } } } return false; }
Comments
Post a Comment