在这个简单的php代码中可能有什么问题

时间:2018-10-12 06:30:50

标签: php

$number1 = 56;
$number2 = 70;
$number3 = 12.56;

echo  "Addition of $number1 and $number2 : " . $number1 + $number2 ;
echo  "<br>";
echo "Subtraction of $number2 from $number1 but with absolute function: " . abs ($number-$number2) . "<br>";

当我刷新页面时,它给出错误“遇到非数值”的任何帮助

2 个答案:

答案 0 :(得分:1)

因为$number未定义。如下定义

$number1 = 56;
$number2 = 70;
$number3 = 12.56;

$number = $number1 + $number2; //define $number
echo  "Addition of $number1 and $number2 : " . $number ;
echo  "<br>";
echo "Subtraction of $number2 from $number but with absolute function: " . abs ($number-$number2) . "<br>";

输出:-https://3v4l.org/kUVRo

或如下更改代码

$number1 = 56;
$number2 = 70;
$number3 = 12.56;

echo  "Addition of $number1 and $number2 : " . ($number1 + $number2) ; //bracket added
echo  "<br>";
//use predefined variable instead of $number
echo "Subtraction of $number2 from $number1 but with absolute function: " . abs ($number1-$number2) . "<br>";

答案 1 :(得分:1)

根据我的评论(您未定义$number。应为$number1,而不是$number)。此外,按照@nick,添加($number1 + $number2)

之类的括号
$number1 = 56;
$number2 = 70;
$number3 = 12.56;

echo  "Addition of $number1 and $number2 : " . ($number1 + $number2) ;
echo  "<br>";
echo "Subtraction of $number2 from $number1 but with absolute function: " . abs ($number1-$number2) . "<br>";