PDA

View Full Version : [PHP] Little Help?



MrCraig
04-01-2008, 03:51 PM
Okay,

I need some code where if a user puts in a "-" sign into the system, it wil bounce back with

Negative numbers are not allowed.


Ive tried both..



<?php
if($bet = "-")
die('Cannot bet a negative number of credits...!<meta http-equiv="refresh" content="2;url=game_hl.php" /><br />You will be redirected!');
?>

And



<?php
if(preg_match("-",$bet))
die('Cannot bet a negative number of credits...!<meta http-equiv="refresh" content="2;url=game_hl.php" /><br />You will be redirected!');
?>


Any ideas?

Moved by Agesilaus (Forum Moderator) from Designing and Development: Please post in the correct forum next time. :)

Blob
04-01-2008, 04:05 PM
<?php
if( eregi("-", $bet) )
{
die('Cannot bet a negative number of credits...!<meta http-equiv="refresh" content="2;url=game_hl.php" /><br />You will be redirected!');
}
?>

MrCraig
04-01-2008, 04:08 PM
Ooh ty, that works :)

+REP if i can.

RYANNNNN
04-01-2008, 05:51 PM
Just use is_numeric

Baving
04-01-2008, 07:18 PM
if($bet == "-") die("Something");



lol..

You used = which is only an assignment operator in PHP. == is to compare

MrCraig
04-01-2008, 08:15 PM
surely == would mean that the character ("-") is the only one in the field?

Mr Macro
05-01-2008, 02:32 PM
surely == would mean that the character ("-") is the only one in the field?

Correct.

There are loads of ways of doing it.


if ($bet < '0'){
die('No negative bets!');
}


Is a way of doing it.

MrCraig
05-01-2008, 02:34 PM
Ooh, never thought of that :P

+REP =]

Assassinator
05-01-2008, 02:39 PM
<?php if($fetch->bet == "-"){
echo "Negative numbers are not allowed.";} ?>

MrCraig
05-01-2008, 02:40 PM
Eh.

Whats with the $fetch-> ?

And surely that would mean that it would only echo the command if the only value in the field was a - sign.

Assassinator
05-01-2008, 02:41 PM
Yes sorry i was thinking of something else.


<?php if(bet == "-"){
echo "Negative numbers are not allowed.";} ?>

Blob
05-01-2008, 07:07 PM
Yes sorry i was thinking of something else.


<?php if(bet == "-"){
echo "Negative numbers are not allowed.";} ?>

That still wont work as you dont have a variable, and even if bet was a function you would need a (), and if your going to use $bet == "-" then it will only work if $bet was just "-", not "-50"

Want to hide these adverts? Register an account for free!