Jump to content


Photo

Comparing Strings


  • Please log in to reply
5 replies to this topic

#1 AirBoss36

AirBoss36
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 August 2006 - 11:17 AM

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.
Per Ardua Ad Astra - Excellentia Super Omnia

#2 Satria Ox41464b

Satria Ox41464b
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 28 August 2006 - 11:26 AM

Your example is comparing Integer.
and that code should be working like what you expect

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 August 2006 - 11:36 AM

That code is fine, although you could write it like this:
<?php
$balance = 100;
$trans = 200;

if ($trans <= $balance)  {

     ***perform transaction***
}

?>

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

Ken

#4 AirBoss36

AirBoss36
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 August 2006 - 11:47 AM

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***
}
?>
Per Ardua Ad Astra - Excellentia Super Omnia

#5 Satria Ox41464b

Satria Ox41464b
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 28 August 2006 - 12:36 PM

make sure what behind $trans and $balance with:
<?php 
var_dump($trans);
var_dump($balance);?>


#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 28 August 2006 - 12:40 PM

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***
}
?>


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

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users