简体   繁体   中英

Operators = vs. == in PHP

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.

== 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

++$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++;
}

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.

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)

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

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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM