(逗号)和之间的区别是什么?(点)作为连接操作符?

时间:2022-02-28 22:31:39

Here is simple PHP code

下面是简单的PHP代码

echo '<form method="POST" action="calcbyme.php"></br>
    enter value1 :<input type="text" name="f1"></br>
    give operator :<input type="text" name="op"></br>
    enter value2 :<input type="text" name="f2"></br>
    <input type="submit" value="calculate"></br>';
    if(isset( $_POST["f1"]) && isset($_POST["f2"]) && isset($_POST["op"]) ){
        $a=$_POST["f1"];
        $b=$_POST["f2"];
        $op=$_POST["op"];
        switch ($op){
            case '+':
                echo "$a+$b=". $a+$b; break;
            case '-':
                echo  "$a-$b=". $a-$b; break;
            case '*':
                echo "$a*$b=". $a*$b; break;
            case '/';
                echo "$a/$b=". $a/$b; break;
            default:
                echo "invalid operator"; break;
        }
    }

If I assume $a=4 and $b=2 but this give only value like this

如果我假设$a=4 $b=2但这个只给出这样的值

6
2
8
2

If I put , (comma) instead of . (dot) then it gives correct output like this

如果我写(逗号)而不是。(点)然后像这样给出正确的输出

4+2=6
4-2=2
4*2=8
4/2=2

Why does this happen?

这为什么会发生?

2 个解决方案

#1


3  

It has to do with php operator precedence...

它与php操作符优先级有关……

Expression containing . is executed before expressions containing +, therefor implicit brackets are:

表达式包含。在包含+的表达式之前执行,其中隐式括号为:

.+- are equal operators (there's no precedence applied on them) and they are executed sequentially from start to end, therefor implicit brackets are:

.+-是相等的运算符(它们不具有优先级),它们从开始到结束依次执行,因此隐式括号为:

echo ("$a+$b=". $a)+$b

So if you want correct output you should use:

所以如果你想要正确的输出,你应该使用:

echo "$a+$b=". ($a+$b)

Empiric examples:

经验主义的例子:

php > echo "foo" . 1 + 2;
// 2
php > echo "foo" . (1 + 2);
// foo3
php > echo 1 + 3 . 'foo';
// 4foo

And why is , working... Because coma separates function arguments and php sees this as:

为什么,工作……因为彗将函数参数分离,php将其视为:

echo( "$a+$b=", $a+$b);

And coma operator (,) is evaluated as last one.

而彗差运算符(,)作为最后一个。

#2


0  

The difference is, that , is not a concatenation operator, but lets start from the beginning

不同之处在于,它不是一个连接操作符,但是让我们从头开始。

echo "$a+$b=". $a+$b;

with $a=2 and $b=4 is evaluated like

对于$a=2和$b=4的取值类似

echo ("4+2=". 4)+2;
echo "4+2=4"+2;
echo ((int) "4+2=4")+2;
echo 4+2;
echo 6

The point is, that the comma , does not concatenate, but it lists expressions (at least for echo. The manual just says "many uses"...), thus every expression is executed on its own.

重点是,逗号不连接,但它列出了表达式(至少是用于echo的)。手册上只写了“许多用法”),因此每个表达式都是自己执行的。

echo "$a+$b=", $a+$b;
echo "$a+$b="; echo $a+$b;
echo "2+4=";echo 2+4;

#1


3  

It has to do with php operator precedence...

它与php操作符优先级有关……

Expression containing . is executed before expressions containing +, therefor implicit brackets are:

表达式包含。在包含+的表达式之前执行,其中隐式括号为:

.+- are equal operators (there's no precedence applied on them) and they are executed sequentially from start to end, therefor implicit brackets are:

.+-是相等的运算符(它们不具有优先级),它们从开始到结束依次执行,因此隐式括号为:

echo ("$a+$b=". $a)+$b

So if you want correct output you should use:

所以如果你想要正确的输出,你应该使用:

echo "$a+$b=". ($a+$b)

Empiric examples:

经验主义的例子:

php > echo "foo" . 1 + 2;
// 2
php > echo "foo" . (1 + 2);
// foo3
php > echo 1 + 3 . 'foo';
// 4foo

And why is , working... Because coma separates function arguments and php sees this as:

为什么,工作……因为彗将函数参数分离,php将其视为:

echo( "$a+$b=", $a+$b);

And coma operator (,) is evaluated as last one.

而彗差运算符(,)作为最后一个。

#2


0  

The difference is, that , is not a concatenation operator, but lets start from the beginning

不同之处在于,它不是一个连接操作符,但是让我们从头开始。

echo "$a+$b=". $a+$b;

with $a=2 and $b=4 is evaluated like

对于$a=2和$b=4的取值类似

echo ("4+2=". 4)+2;
echo "4+2=4"+2;
echo ((int) "4+2=4")+2;
echo 4+2;
echo 6

The point is, that the comma , does not concatenate, but it lists expressions (at least for echo. The manual just says "many uses"...), thus every expression is executed on its own.

重点是,逗号不连接,但它列出了表达式(至少是用于echo的)。手册上只写了“许多用法”),因此每个表达式都是自己执行的。

echo "$a+$b=", $a+$b;
echo "$a+$b="; echo $a+$b;
echo "2+4=";echo 2+4;