我一直在尝试查看一个数字是负数还是正面,并将其标记为费用或佣金。以下代码是我到目前为止所拥有的
$commm = $_POST['com_fee'];
$findme = '-';
$pos = strpos($commm, $findme);
if ($pos === false) {
$comfee = 'Fee';
}
else {
$comfee = 'Commission';
}
由于某种原因,$comfee
始终被定义为"费用"。谁能告诉我我做错了什么?
只需检查$ _post ['com_fee']是否存在,是数字,以及是否> = 0
//$commm = $_POST['com_fee'];
$commm_list = [ '',
'kjhgfd',
'jhgf-dcfvgb',
'-',
'-1',
'0',
'+1',
];
function from_stf( $commm ) {
$findme = '-';
$pos = strpos($commm, $findme);
if ($pos === false) {
$comfee= 'Fee';
} else{$comfee='Commission';}
return $comfee;
}
foreach ($commm_list as $commm) {
$res = from_stf( $commm );
print $commm.' --> '.$res."n";
}
和结果:
$ php ./wiksphp/new.php
--> Fee
kjhgfd --> Fee
jhgf-dcfvgb --> Commission
- --> Commission
-1 --> Commission
0 --> Fee
+1 --> Fee
$
而不是寻找' - '符号,您不能仅检查数字是否为负?
$comm = $_POST['com_fee'];
$comfee = $comm < 0 ? 'Commission' : 'Fee';