Jump to content

Archived

This topic is now archived and is closed to further replies.

AirBoss36

Comparing Strings

Recommended Posts

I want to compare two strings for an if statement.  With something like a bank account, I want it to check and see if there is enough money in the account before it performs the transaction.  Here is what I've tried:

$balance = 100;
$trans = 200;

if (($trans < $balance) || ($trans == $balance)) {

    ***perform transaction***
}

?>

<html>..... Msg stating not enough money in the account.

In the scenario, it should not perform the transaction, and then should carry on to the html to say that there is not enough money in the account.  At least, that's what i want it to do. 

Right now, it processes the transaction even though its greater than the balance.  What am I doing wrong?

Thanks.

Share this post


Link to post
Share on other sites
That code is fine, although you could write it like this:
[code]<?php
$balance = 100;
$trans = 200;

if ($trans <= $balance)  {

    ***perform transaction***
}

?>[/code]

For us to figure out your problem, we would need more information.

Ken

Share this post


Link to post
Share on other sites
If I'm using the statement correctly, why would it process the transaction if the balance is less than the transaction?

Should I attack from the other direction?

if ($trans > $balance) {

echo "Not enough money";

exit();

}

else {

*** process transaction***
}
?>

Share this post


Link to post
Share on other sites
[quote author=AirBoss36 link=topic=105921.msg423269#msg423269 date=1156765653]
If I'm using the statement correctly, why would it process the transaction if the balance is less than the transaction?

Should I attack from the other direction?

if ($trans > $balance) {

echo "Not enough money";

exit();

}

else {

*** process transaction***
}
?>
[/quote]

yes, you should be attacking it from this new direction. basically, you want to say that if the transaction is [b]more[/b] than their balance, you don't want to let them do it:
[code]
<?php
if ($trans > $bal) {
  // too much, don't process
} else {
  // valid request, process it
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.