PHP Operators

PHP Operators

In PHP, operators are symbols or keywords used to perform operations on variables and values. PHP supports a variety of operators, including arithmetic, comparison, logical, assignment, and more. Let's dive into the different categories of operators in PHP:

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

OperatorDescriptionExample
+Addition$a + $b (adds $a and $b)
-Subtraction$a - $b (subtracts $b from $a)
*Multiplication$a * $b (multiplies $a and $b)
/Division$a / $b (divides $a by $b)
%Modulus (Remainder)$a % $b (remainder when dividing $a by $b)
**Exponentiation$a ** $b (a raised to the power of b)

Example:

$a = 10; $b = 3; echo $a + $b; // Outputs: 13 echo $a - $b; // Outputs: 7 echo $a * $b; // Outputs: 30 echo $a / $b; // Outputs: 3.33333 echo $a % $b; // Outputs: 1 echo $a ** $b; // Outputs: 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is =, but PHP also supports combined assignment operators for arithmetic operations.

OperatorDescriptionExample
=Simple assignment$a = 5
+=Addition assignment$a += 5 (equivalent to $a = $a + 5)
-=Subtraction assignment$a -= 5 (equivalent to $a = $a - 5)
*=Multiplication assignment$a *= 5 (equivalent to $a = $a * 5)
/=Division assignment$a /= 5 (equivalent to $a = $a / 5)
%=Modulus assignment$a %= 5 (equivalent to $a = $a % 5)
.= Concatenation assignment$a .= " world" (equivalent to $a = $a . " world")

Example:

$a = 5; $a += 3; // $a = 5 + 3 = 8 $a -= 2; // $a = 8 - 2 = 6 $a *= 2; // $a = 6 * 2 = 12 $a /= 3; // $a = 12 / 3 = 4 $a .= " world"; // $a = "4 world"

3. Comparison Operators

Comparison operators are used to compare two values. These operators return a boolean value (true or false).

OperatorDescriptionExample
==Equal to$a == $b (true if $a equals $b)
===Identical (equal and same type)$a === $b (true if $a equals $b and is of the same type)
!=Not equal to$a != $b (true if $a is not equal to $b)
!==Not identical$a !== $b (true if $a is not equal to $b or not the same type)
>Greater than$a > $b (true if $a is greater than $b)
<Less than$a < $b (true if $a is less than $b)
>=Greater than or equal to$a >= $b (true if $a is greater than or equal to $b)
<=Less than or equal to$a <= $b (true if $a is less than or equal to $b)

Example:

$a = 5; $b = 3; var_dump($a == $b); // Outputs: bool(false) var_dump($a != $b); // Outputs: bool(true) var_dump($a > $b); // Outputs: bool(true) var_dump($a < $b); // Outputs: bool(false) var_dump($a === 5); // Outputs: bool(true) var_dump($a !== '5'); // Outputs: bool(true)

4. Logical Operators

Logical operators are used to combine multiple conditions and return a boolean value.

OperatorDescriptionExample
&&And (true if both sides are true)$a && $b (true if both $a and $b are true)
``
!Not (true if the condition is false)!$a (true if $a is false)
andLogical AND (same as &&)$a and $b (true if both $a and $b are true)
orLogical OR (same as `

Example:

$a = true; $b = false; var_dump($a && $b); // Outputs: bool(false) var_dump($a || $b); // Outputs: bool(true) var_dump(!$a); // Outputs: bool(false)

5. Increment/Decrement Operators

Increment and decrement operators are used to increase or decrease a variable's value by 1.

OperatorDescriptionExample
++Increment (adds 1 to the value)$a++ or ++$a (increases $a by 1)
--Decrement (subtracts 1 from the value)$a-- or --$a (decreases $a by 1)
  • Post-increment ($a++): Increases the value of $a after the expression is evaluated.
  • Pre-increment (++$a): Increases the value of $a before the expression is evaluated.

Example:

$a = 5; echo $a++; // Outputs: 5 (then $a becomes 6) echo ++$a; // Outputs: 7 (before the expression is evaluated, $a becomes 7)

6. String Operators

PHP also has a special operator for string manipulation.

OperatorDescriptionExample
.Concatenation$a . $b (concatenates $a and $b)
.=Concatenation assignment$a .= $b (appends $b to $a)

Example:

$a = "Hello"; $b = "World"; echo $a . " " . $b; // Outputs: Hello World $a .= $b; // Appends $b to $a echo $a; // Outputs: HelloWorld

7. Array Operators

Array operators are used to perform operations on arrays.

OperatorDescriptionExample
+Union of two arrays$a + $b (returns array $a merged with array $b)
==Equality (true if arrays are equal)$a == $b (true if $a and $b have the same keys and values)
===Identity (true if arrays are identical)$a === $b (true if $a and $b have the same keys, values, and order)
!=Inequality$a != $b (true if $a and $b are not equal)
!==Non-identity$a !== $b (true if $a and $b are not identical)

Example:

$a = [1, 2, 3]; $b = [1, 2, 3]; var_dump($a == $b); // Outputs: bool(true) var_dump($a === $b); // Outputs: bool(true) $c = [1, 2]; $d = [1, 2, 3]; var_dump($c + $d); // Outputs: array(1, 2, 3) (array $d's elements are merged)

8. Ternary Operator

The ternary operator is a shorthand for the if-else statement. It is used to return one of two values depending on a condition.

Syntax:

condition ? value_if_true : value_if_false;

Example:

$a = 5; echo $a > 3 ? 'Greater' : 'Smaller'; // Outputs: Greater

Conclusion

  • PHP operators are crucial for performing a variety of tasks, such as arithmetic, comparison, logical operations, string manipulation, and more.
  • Understanding these operators is essential for building dynamic and functional PHP applications.
  • You can combine these operators to create complex expressions and control the flow of your application logic.
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close