停止为PHP提供错误number_format

时间:2017-04-05 16:11:37

标签: php if-statement error-handling number-formatting

以下是问题:

$var = 1000;
$var2 = number_format($var,2);// No error

$var = 'Some String';
$var2 = number_format($var,2);// Gives 'number_format() expects parameter 1 to be double.... error'

我的处理方案是;

$var = 'Some String';
$var_escape = 1000;
if(!$var2 = number_format($var,2)){
    $var2 = $var_escape;// if $var not a integer; always give 1000 to $var2.
}

这个解决方案工作得很好但是;仍然给出“期望参数错误”;因为这一行:

  

if(!$ var2 = number_format($ var,2)){

我不想使用“ @ ”抑制。这个问题还有其他解决方案吗?感谢...

我通过答案解决方案启发

if(!is_numeric($var2 = $var)){$var2 = $var_escape;}

谢谢大家......

4 个答案:

答案 0 :(得分:2)

您可以使用is_numeric函数

检查您的输入是否为数字
$var = 'Some String';
$var_escape = 1000;
if(is_numeric($var))
$var2=number_format($var,2);
else
$var2=$var_escape;

答案 1 :(得分:0)

您只需使用默认值提前声明$var2,然后使用PHP中的内置$var函数检查以确保is_numeric()的传入值为数字。如果$var的传入值确实是数字,则将该数值分配给$var2,否则$var2将保留为默认值1000

$var2 = 1000;

if(is_numeric($var)){
  $var2 = number_format($var,2);
}

// You can proceed to use $var2 here as it will be 1000 or the value from $var if it was numeric.

答案 2 :(得分:0)

使用正则表达式将是最简单的,这将验证它是整数还是浮点数。

  

$vars = array(
    'Some String',
    123,
    123.5,
    -11,
    "123.55",
    "$2!!",
    .1,
    1,
    ".1",
    "1"
);

foreach($vars as $var) {

    if(0 !== preg_match('/^-?\d*\.?\d*$/', $var)) {
        echo number_format($var, 2) . PHP_EOL;
    } else {
        echo $var . " -> " . 1000 . PHP_EOL;
    }
}
  

输出

Some String -> 1000
123.00
123.50
-11.55
123.55
$2!! -> 1000
0.10
1.00
0.10
1.00

答案 3 :(得分:0)

小心...... Demo

使用:

root@ns205125:~# ffmpeg -i /var/www/hespress/intro.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts /var/www/hespress/17/intermediate1.ts
ffmpeg version 0.8.20-6:0.8.20-0+deb7u1, Copyright (c) 2000-2014 the Libav developers
  built on Jan 19 2017 11:13:36 with gcc 4.7.2
The ffmpeg program is only provided for script compatibility and will be removed
in a future release. It has been deprecated in the Libav project to allow for
incompatible command line syntax improvements in its replacement called avconv
(see Changelog for details). Please use avconv instead.
[aac @ 0x3938ba0] Input buffer exhausted before END element found

Seems stream 0 codec frame rate differs from container frame rate: 180000.00 (180000/1) -> 30.00 (30/1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/www/hespress/intro.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2017-03-27 23:03:04
  Duration: 00:00:12.92, start: 0.000000, bitrate: 2598 kb/s
    Stream #0.0(eng): Video: h264 (Baseline), yuv420p, 1280x720, 2479 kb/s, 30 fps, 30 tbr, 90k tbn, 180k tbc
    Metadata:
      creation_time   : 2017-03-27 23:03:04
    Stream #0.1(eng): Audio: aac, 48000 Hz, mono, s16, 127 kb/s
    Metadata:
      creation_time   : 2017-03-27 23:03:04
Unrecognized option 'c'
Failed to set value 'copy' for option 'c'
root@ns205125:~# 

而不是:

if(is_float($var) || is_integer($var)){

除非您希望处理数字字符串。