You are on page 1of 2

Question: Discuss different Operators available in PHP.

Answer:
1. Arithmetic Operators:
- Used for basic mathematical operations.
- Example:
<?php
$a = 10;
$b = 5;
$sum = $a + $b; // Addition
$difference = $a - $b; // Subtraction
$product = $a * $b; // Multiplication
$quotient = $a / $b; // Division
$remainder = $a % $b; // Modulus
?>

2. Assignment Operators:
- Used to assign values to variables.
- Example:
<?php
$x = 10;
$y = 5;
$x += $y; // Equivalent to $x = $x + $y;
?>

3. Comparison Operators:
- Used to compare values and return true or false.
- Example:

<?php
$a = 10;
$b = 5;
$isEqual = $a == $b; // Equal to
$isNotEqual = $a != $b; // Not equal to
$isGreater = $a > $b; // Greater than
?>

4. Logical Operators:
- Used to combine and manipulate conditions.
- Example:
<?php
$x = true;
$y = false;
$result = $x && $y; // Logical AND
$result2 = $x || $y; // Logical OR
$result3 = !$x; // Logical NOT
?>

5. Increment/Decrement Operators:
- Used to increase or decrease a variable's value.
- Example:
<?php
$count = 5;
$count++; // Increment by 1
$count--; // Decrement by 1
?>

6. String Concatenation Operator:


- Used to combine two strings.
- Example:
<?php
$first_name = "John";
$last_name = "Doe";
$full_name = $first_name . " " . $last_name; ?>

7. Conditional (Ternary) Operator:


- Provides a concise way to express conditional statements.
- Example:
<?php
$age = 25;
$isAdult = ($age >= 18) ? "Yes" : "No";
?>

You might also like