Operators = vs. == in PHP

和自甴很熟 提交于 2021-02-17 06:54:07

问题


I am learning basic PHP from a book, and from what I read, = is an assignment operator, and == is a comparison operator. So...

$x = 5;
$x == 5: true

...makes sense. However, the book gives an example which confuses me:

if (++$x == 10)
    echo $x;

Why ==? Aren't we trying to say "if ++$x equals 10, then echo $x"...? Then that would seem like: if (++$x = 10). The former would be like asking a question inside a conditional statement, which would be illogical or redundant.


回答1:


== means equality, so the conditional reads as:

If pre-incremented $x equals 10, echo $x

Single = is assignment, where a variable is set to contain a value:

$word = 'hello';
$number = 5;
// etc.

echo "I said $word $number times!";

Regarding the increment opperators:

You'll see things like ++$x and $i-- as you learn PHP (and/or other languages). These are increment/decrement operators. Where they're positioned in relation to the variable they're operating on is important.

If they're placed before the variable, like ++$x, it's a pre-increment/decrement. This means the operation is performed before anything else can be done to the variable. If it's placed after, like $x++, it's a post-increment/decrement, and it means that the operation is performed afterward.

It's easiest to see in an example script:

$x = 5;

echo ++$x; // 6
echo $x++; // ALSO 6
echo $x; // NOW 7



回答2:


We always ask questions in conditional statements, and act on whether the result is true or false. If you said ++$x=10, you'd be saying "Add one to X, and then SET the value of X to be 10." Instead you want to know IF after we add one to X IS it equal to 10.

= means SET the expression on the left to be the value on the right == means TELL ME TRUE if the expression on the left is the same as the value on the right




回答3:


I would recommend NOT incrementing/decrementing a variable within an if statement... it's not good for code readability. In any case, you're still doing a comparison here, you're just ALSO incrementing the value of X.

Consider it this way:

if((++$x) == 10) 

You have an expression on the left of the '==', which you are comparing to an expression on the right side. The ++$x is evaluated first, then the result of that is compared to 10.




回答4:


++$x is executed before the condition itself is evalutated, so this condition would be true:

$x = 9;
if (++$x == 10) {
    //this is executed
}

This can be used to build very short while loops:

// Count from 1 to 10
$i = 0
while ($i <= 10) {
    echo ++$i;
}

The difference between $i++ and ++$i is the order of incrementing and evaluation:

// Count from 0 to 9
$i = 0
while ($i <= 10) {
    echo $i++;
}



回答5:


The syntax = is assignment:

$Var = "String"; // $Var is now set to contain "string"

Whereas

== is a comparison operator:

if ($Var == "Value){
  // Since var is set to "string", the comparison fails

}

Your example:

if (++$x == 10){

}

Is the same as doing this:

 $i = 0;
 while ($i < $x){
  if ($x == 10){
   break;
  }
   ++$x;
 }



回答6:


What the book says is: If a variable is true at 10 then write the variable out. The if statement evaluates whether the condition is true or false.. so

   if ($x == 10) 

is equally true when checked as:

   $x = 9
   if (++$x == 10)


来源:https://stackoverflow.com/questions/20205338/operators-vs-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!