PHP Tutorial



OPERATORS IN PHP


🔧 PHP Operators

In PHP, operators are used to perform operations on variables and values. Operators allow you to perform various tasks like arithmetic, comparison, logical operations, and more. Understanding the different types of operators in PHP will help you manipulate and work with data effectively.

💡 Tip: Operators are crucial in PHP to build complex expressions that can handle various data types and operations.

📘 Types of PHP Operators

  • Arithmetic Operators - Used to perform arithmetic operations like addition, subtraction, multiplication, etc.
  • Assignment Operators - Used to assign values to variables.
  • Comparison Operators - Used to compare two values.
  • Logical Operators - Used to perform logical operations.
  • Increment/Decrement Operators - Used to increase or decrease a variable's value by 1.
  • String Operators - Used to manipulate strings.
  • Array Operators - Used to compare or combine arrays.
  • Conditional (Ternary) Operator - A shorthand for an `if-else` statement.
  • Execution Operator - Used to execute commands from the system.

➗ Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

<?php
  $a = 10;
  $b = 5;
  
  echo $a + $b;  // Addition (15)
  echo $a - $b;  // Subtraction (5)
  echo $a * $b;  // Multiplication (50)
  echo $a / $b;  // Division (2)
  echo $a % $b;  // Modulus (0)
  echo $a ** $b; // Exponentiation (100000)
?>
  

💼 Assignment Operators

Assignment operators are used to assign values to variables. These operators simplify value assignment.

<?php
  $x = 10;  // Basic assignment
  $x += 5;  // Addition assignment (x = x + 5)
  $x -= 3;  // Subtraction assignment (x = x - 3)
  $x *= 2;  // Multiplication assignment (x = x * 2)
  $x /= 4;  // Division assignment (x = x / 4)
  echo $x;
?>
  

⚖️ Comparison Operators

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

<?php
  $x = 10;
  $y = 20;
  
  var_dump($x == $y); // Equal to
  var_dump($x != $y); // Not equal to
  var_dump($x > $y);  // Greater than
  var_dump($x < $y);  // Less than
  var_dump($x >= $y); // Greater than or equal to
  var_dump($x <= $y); // Less than or equal to
?>
  

🔗 Logical Operators

Logical operators are used to perform logical operations, commonly used in `if` statements.

<?php
  $x = true;
  $y = false;
  
  var_dump($x && $y);  // Logical AND (false)
  var_dump($x || $y);  // Logical OR (true)
  var_dump(!$x);       // Logical NOT (false)
?>
  

🔼 Increment/Decrement Operators

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

<?php
  $x = 5;
  echo ++$x; // Pre-increment (6)
  echo $x++; // Post-increment (6, then 7)
  echo --$x; // Pre-decrement (6)
  echo $x--; // Post-decrement (6, then 5)
?>
  

📝 String Operators

String operators are used to combine or append strings.

<?php
  $str1 = "Hello ";
  $str2 = "World!";
  
  echo $str1 . $str2; // Concatenation ("Hello World!")
  $str1 .= $str2;      // Concatenation assignment
  echo $str1;          // ("Hello World!")
?>
  

🔢 Array Operators

Array operators are used to compare arrays.

<?php
  $arr1 = array("a" => "apple", "b" => "banana");
  $arr2 = array("a" => "apple", "c" => "cherry");
  
  var_dump($arr1 + $arr2);  // Union of arrays
  var_dump($arr1 == $arr2); // Arrays are equal?
  var_dump($arr1 === $arr2); // Arrays are identical?
?>
  

❓ Conditional (Ternary) Operator

The ternary operator is a shorthand for an `if-else` statement.

<?php
  $x = 5;
  $result = ($x > 3) ? "Greater" : "Smaller";
  echo $result;  // Output: Greater
?>
  

✅ Summary:

  • Operators are essential in PHP for performing operations on variables and values.
  • They help you perform arithmetic, logical, comparison, string manipulation, and array operations.
  • Learn to use different operators efficiently to create more dynamic and interactive PHP code.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review