value greater or less than.. confused
I have a very simple statement that checks whether a value is less than the other, but it does not work, anyone who can check it quickly? Have seen me completely blind to this.
$balance = $wallet->balance(); //3000
$loan = $wallet->loan(); // 5000
if (!$balance < $loan) { //Should be pretty straight forward...
$wallet->updateBalance(Session::get('user'),$balance - 1000);
$wallet->updateLoan(Session::get('user'),$loan - 1000);
Redirect::to('bank.php');
} else {
Redirect::to('bank.php');
}
When I run this code it removes 1000 regardless if $balance is less. If I remove the exclamation mark it redirects right away, as it should.
I really do not understand what I'm doing wrong?
Here is the complete script:
<?php
require_once 'core/init.php';
if (Input::exists('get')) {
if (Input::get('borrow')) {
$wallet = new Wallet;
if ($wallet->get(Session::get('user'))) {
$balance = $wallet->balance();
$loan = $wallet->loan();
$wallet->updateBalance(Session::get('user'),$balance + 1000);
$wallet->updateLoan(Session::get('user'),$loan + 1000);
Redirect::to('bank.php');
}
} else if (Input::get('repay')) {
$wallet = new Wallet;
if ($wallet->get(Session::get('user'))) {
$balance = $wallet->balance();
$loan = $wallet->loan();
if ($balance < $loan) {
$wallet->updateBalance(Session::get('user'),$balance - 1000);
$wallet->updateLoan(Session::get('user'),$loan - 1000);
Redirect::to('bank.php');
} else {
Redirect::to('bank.php');
}
}
} else {
Redirect::to('bank.php');
}
} else {
Redirect::to('bank.php');
}
All redirects are temporary .. Help is greatly appreciated
I believe the problem is with how operators are being applied in your if condition. Instead of if (!$balance < $loan)
, try if (!($balance < $loan))
or if ($balance >= $loan)
The problem is partly because of how PHP represents types internally, and partly because of operator precedence. The negation operator is being applied first. Because bools are represented by integers in PHP, the result of this expression is FALSE
, which is equivalent to 0
.
上一篇: PHP如果Post Value小于或大于
下一篇: 价值大于或小于..困惑