2017-11-25 78 views
0

我一直在试图查看一个数字是否定的或积极的,并将其标记为费用或佣金。下面的代码是我到目前为止,

$commm = $_POST['com_fee']; 
$findme = '-'; 
$pos = strpos($commm, $findme); 

if ($pos === false) { 
    $comfee = 'Fee'; 
} 
else { 
    $comfee = 'Commission'; 
} 

出于某种原因,$comfee总是被定义为“服务费”。谁能告诉我我做错了什么?

+0

如果小于零,则数字为负数。 – FirstOne

+0

这似乎不可复制。 – FirstOne

+0

请编辑您的问题以在'if'语句之前包含'var_dump($ pos,$ comm,$ findme,$ _POST);'的输出。 – Progman

回答

0

只需检查是否$ _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 
$ 
+1

我在发布数据时发生错误。谢谢! –

0

而不是寻找的 ' - ' 符号你不能只检查一下数字是否为负数?

$comm = $_POST['com_fee']; 

$comfee = $comm < 0 ? 'Commission' : 'Fee';