Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将字符串乘以整数_Php - Fatal编程技术网

Php 将字符串乘以整数

Php 将字符串乘以整数,php,Php,我需要用PHP显示一个分数。为此,我写道: $a=3 $b=2 $c=“$a/$b” echo$c;//显示3/2 但另一方面,我想把$c乘以一个整数 回声$c*2;//这显示了一个错误 这就是我看到的:(!)注意:遇到了格式不正确的数值 有人能帮我吗? 删除围绕操作的双引号($a/$b),因为这意味着它是一个字符串,而不是整数或浮点 $a = 3; $b = 2; $c = $a/$b; echo $c * 2; 显示分数是另一回事 也许看看这个 或者使用这个功能 function

我需要用PHP显示一个分数。为此,我写道:

$a=3

$b=2

$c=“$a/$b”

echo$c;//显示3/2 但另一方面,我想把$c乘以一个整数

回声$c*2;//这显示了一个错误

这就是我看到的:(!)注意:遇到了格式不正确的数值

有人能帮我吗?

  • 删除围绕操作的双引号(
    $a/$b
    ),因为这意味着它是一个字符串,而不是整数或浮点

    $a = 3;
    $b = 2;
    $c = $a/$b; 
    echo $c * 2;
    
  • 显示分数是另一回事

也许看看这个

  • 或者使用这个功能

    function decimalToFraction($decimal){
    
        if ($decimal < 0 || !is_numeric($decimal)) {
            // Negative digits need to be passed in as positive numbers
            // and prefixed as negative once the response is imploded.
            return false;
        }
        if ($decimal == 0) {
            return [0, 0];
        }
    
        $tolerance = 1.e-4;
    
        $numerator = 1;
        $h2 = 0;
        $denominator = 0;
        $k2 = 1;
        $b = 1 / $decimal;
        do {
            $b = 1 / $b;
            $a = floor($b);
            $aux = $numerator;
            $numerator = $a * $numerator + $h2;
            $h2 = $aux;
            $aux = $denominator;
            $denominator = $a * $denominator + $k2;
            $k2 = $aux;
            $b = $b - $a;
        } while (abs($decimal - $numerator / $denominator) > $decimal * $tolerance);
    
        return [
            $numerator,
            $denominator
        ];
    }
    
    函数十进制操作($decimal){
    如果($decimal<0 | |!是数字($decimal)){
    //负数需要作为正数传入
    //一旦响应内爆,前缀为负值。
    返回false;
    }
    如果($decimal==0){
    返回[0,0];
    }
    $公差=1.e-4;
    $分子=1;
    $h2=0;
    $分母=0;
    $k2=1;
    $b=1/$decimal;
    做{
    $b=1/$b;
    $a=底价($b);
    $aux=$momerator;
    $numerator=$a*$numerator+$h2;
    $h2=$aux;
    $aux=$denominator;
    $denominator=$a*$denominator+$k2;
    $k2=$aux;
    $b=$b-$a;
    }而(绝对值($十进制-$分子/$分母)>$十进制*$公差);
    返回[
    $分子,
    美元分母
    ];
    }
    

来源

非常感谢