Operators - potatoscript/php GitHub Wiki
Operators in PHP
Overview
Operators in PHP are used to perform various operations on variables and values. PHP supports different types of operators, including arithmetic, assignment, comparison, logical, and more. Understanding these operators is crucial for performing basic operations and controlling the flow of your PHP scripts.
In this section, we will cover the following types of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Increment/Decrement Operators
- String Operators
- Array Operators
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | $a + $b |
- |
Subtraction | $a - $b |
* |
Multiplication | $a * $b |
/ |
Division | $a / $b |
% |
Modulus (remainder) | $a % $b |
** |
Exponentiation (since PHP 5.6) | $a ** $b |
Example:
<?php
$a = 10;
$b = 5;
echo $a + $b; // Output: 15
echo $a - $b; // Output: 5
echo $a * $b; // Output: 50
echo $a / $b; // Output: 2
echo $a % $b; // Output: 0
echo $a ** $b; // Output: 100000
?>
Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Assigns the right-hand value to the left-hand variable | $a = $b |
+= |
Adds right-hand value to the left-hand variable | $a += $b (equivalent to $a = $a + $b ) |
-= |
Subtracts right-hand value from the left-hand variable | $a -= $b (equivalent to $a = $a - $b ) |
*= |
Multiplies left-hand variable by the right-hand value | $a *= $b (equivalent to $a = $a * $b ) |
/= |
Divides left-hand variable by the right-hand value | $a /= $b (equivalent to $a = $a / $b ) |
%= |
Assigns the remainder of left-hand variable divided by right-hand value | $a %= $b (equivalent to $a = $a % $b ) |
Example:
<?php
$a = 10;
$a += 5; // $a is now 15
$a -= 3; // $a is now 12
$a *= 2; // $a is now 24
$a /= 4; // $a is now 6
?>
Comparison Operators
Comparison operators are used to compare two values and return a boolean result (true
or false
).
Operator | Description | Example |
---|---|---|
== |
Equal to | $a == $b |
=== |
Identical (equal and of the same type) | $a === $b |
!= |
Not equal to | $a != $b |
!== |
Not identical (not equal or not the same type) | $a !== $b |
> |
Greater than | $a > $b |
< |
Less than | $a < $b |
>= |
Greater than or equal to | $a >= $b |
<= |
Less than or equal to | $a <= $b |
Example:
<?php
$a = 5;
$b = 10;
var_dump($a == $b); // Output: bool(false)
var_dump($a != $b); // Output: bool(true)
var_dump($a < $b); // Output: bool(true)
var_dump($a === "5"); // Output: bool(false)
?>
Logical Operators
Logical operators are used to combine multiple conditions or boolean expressions.
Operator | Description | Example |
---|---|---|
&& |
Logical AND (true if both conditions are true) | $a && $b |
` | ` | |
! |
Logical NOT (inverts the condition) | !$a |
Example:
<?php
$a = true;
$b = false;
var_dump($a && $b); // Output: bool(false)
var_dump($a || $b); // Output: bool(true)
var_dump(!$a); // Output: bool(false)
?>
Increment and Decrement Operators
Increment and Decrement operators are used to increase or decrease the value of a variable by 1.
Operator | Description | Example |
---|---|---|
++ |
Increment (increase by 1) | $a++ (post-increment) or ++$a (pre-increment) |
-- |
Decrement (decrease by 1) | $a-- (post-decrement) or --$a (pre-decrement) |
Example:
<?php
$a = 5;
echo $a++; // Output: 5 (Post-increment: value is used first, then incremented)
echo ++$a; // Output: 7 (Pre-increment: value is incremented first, then used)
$b = 10;
echo $b--; // Output: 10 (Post-decrement)
echo --$b; // Output: 8 (Pre-decrement)
?>
String Operators
String operators are used to manipulate strings.
Operator | Description | Example |
---|---|---|
. |
Concatenation (combine two strings) | $a . $b |
.= |
Concatenate and assign | $a .= $b |
Example:
<?php
$greeting = "Hello";
$name = "Lucy";
echo $greeting . " " . $name; // Output: Hello Lucy
$greeting .= " there!"; // $greeting is now "Hello there!"
?>
Array Operators
Array operators are used to perform operations on arrays.
Operator | Description | Example |
---|---|---|
+ |
Union (combines two arrays) | $array1 + $array2 |
== |
Equality (returns true if arrays have the same key/value pairs) | $array1 == $array2 |
=== |
Identity (returns true if arrays have the same key/value pairs in the same order) | $array1 === $array2 |
!= |
Inequality (returns true if arrays are not equal) | $array1 != $array2 |
!== |
Non-identity (returns true if arrays are not identical) | $array1 !== $array2 |
Example:
<?php
$array1 = array("a" => "apple", "b" => "banana");
$array2 = array("a" => "apple", "b" => "banana");
var_dump($array1 == $array2); // Output: bool(true)
var_dump($array1 === $array2); // Output: bool(true)
?>
Conclusion
In this section, we explored the various types of operators in PHP, including arithmetic, assignment, comparison, logical, increment/decrement, string, and array operators. These operators allow you to manipulate variables and control the flow of your code. Understanding and utilizing these operators is fundamental to writing efficient and functional PHP scripts.
In the next section, we will dive deeper into control structures like conditionals and loops to further enhance your PHP skills.