📜  PHP之operators(1)

📅  最后修改于: 2023-12-03 14:45:28.644000             🧑  作者: Mango

PHP之Operators

Operators are symbols or words used to perform operations on variables and values in PHP. PHP provides a wide range of operators, including:

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations in PHP.

| Operator | Description | | -------- | ----------- | | + | Addition | | - | Subtraction | | * | Multiplication | | / | Division | | % | Modulus (remainder after division) |

Examples:

$x = 10;
$y = 5;

echo $x + $y; // Outputs 15
echo $x - $y; // Outputs 5
echo $x * $y; // Outputs 50
echo $x / $y; // Outputs 2
echo $x % $y; // Outputs 0
Assignment Operators

Assignment operators are used to assign values to variables in PHP.

| Operator | Description | | -------- | ----------- | | = | Assign the value on the right to the variable on the left | | += | Add the value on the right to the variable on the left and assign the result to the variable on the left | | -= | Subtract the value on the right from the variable on the left and assign the result to the variable on the left | | *= | Multiply the value on the right with the variable on the left and assign the result to the variable on the left | | /= | Divide the variable on the left by the value on the right and assign the result to the variable on the left | | %= | Take the modulus of the variable on the left and the value on the right and assign the result to the variable on the left |

Examples:

$x = 10;

$x += 5; // Same as $x = $x + 5
echo $x; // Outputs 15

$x -= 3; // Same as $x = $x - 3
echo $x; // Outputs 12

$x *= 2; // Same as $x = $x * 2
echo $x; // Outputs 24

$x /= 4; // Same as $x = $x / 4
echo $x; // Outputs 6

$x %= 5; // Same as $x = $x % 5
echo $x; // Outputs 1
Comparison Operators

Comparison operators are used to compare values in PHP.

| Operator | Description | | -------- | ----------- | | == | Check if the values on both sides are equal | | != | Check if the values on both sides are not equal | | > | Check if the value on the left is greater than the value on the right | | < | Check if the value on the left is less than the value on the right | | >= | Check if the value on the left is greater than or equal to the value on the right | | <= | Check if the value on the left is less than or equal to the value on the right |

Examples:

$x = 10;
$y = 5;

var_dump($x == $y); // Outputs bool(false)
var_dump($x != $y); // Outputs bool(true)
var_dump($x > $y); // Outputs bool(true)
var_dump($x < $y); // Outputs bool(false)
var_dump($x >= $y); // Outputs bool(true)
var_dump($x <= $y); // Outputs bool(false)
Logical Operators

Logical operators are used to combine or negate conditions in PHP.

| Operator | Description | | -------- | ----------- | | && or and | Returns true if both conditions on either side of the operator are true | | || or or | Returns true if at least one of the conditions on either side of the operator is true | | ! or not | Returns true if the condition to the right of the operator is false |

Examples:

$x = 10;
$y = 5;
$z = 3;

// Using && (and)
if ($x > $y && $x > $z) {
  echo "x is the greatest number";
}

// Using || (or)
if ($x > $y || $y > $z) {
  echo "At least one of the conditions is true";
}

// Using ! (not)
if (!($x < $y)) {
  echo "x is not less than y";
}
Increment/Decrement Operators

Increment/decrement operators are used to increase or decrease the value of a variable by 1 in PHP.

| Operator | Description | | -------- | ----------- | | ++$x | Increase $x by 1 and then return the new value of $x | | $x++ | Return the current value of $x and then increase $x by 1 | | --$x | Decrease $x by 1 and then return the new value of $x | | $x-- | Return the current value of $x and then decrease $x by 1 |

Examples:

$x = 10;

echo ++$x; // Outputs 11
echo $x++; // Outputs 11 (the new value of $x is 12)
echo $x; // Outputs 12

echo --$x; // Outputs 11
echo $x--; // Outputs 11 (the new value of $x is 10)
echo $x; // Outputs 10
String Operators

String operators are used to manipulate strings in PHP.

| Operator | Description | | -------- | ----------- | | . | Concatenate two strings | | .= | Append the string on the right to the string on the left |

Examples:

$hello = "Hello, ";
$world = "world!";

echo $hello . $world; // Outputs "Hello, world!"

$name = "John";
$name .= " Doe";
echo $name; // Outputs "John Doe"
Array Operators

Array operators are used to manipulate arrays in PHP.

| Operator | Description | | -------- | ----------- | | + | Union of two arrays | | == | Returns true if the arrays on both sides have the same key/value pairs | | === | Returns true if the arrays on both sides have the same key/value pairs in the same order and of the same types |

Examples:

$array1 = array("a" => "apple", "b" => "banana");
$array2 = array("c" => "cherry", "d" => "date");
$array3 = $array1 + $array2;
print_r($array3); // Outputs Array([a] => apple [b] => banana [c] => cherry [d] => date)

$array4 = array("a" => "apple", "b" => "banana");
$array5 = array("b" => "banana", "a" => "apple");
var_dump($array4 == $array5); // Outputs bool(true)

$array6 = array("a" => "apple", "b" => "banana");
$array7 = array("b" => "banana", "a" => "apple");
var_dump($array6 === $array7); // Outputs bool(false)
Conclusion

In conclusion, PHP provides a wide range of operators to perform various operations on variables and values. Understanding these operators is crucial to writing effective PHP code.