PHP Operators


Operators in PHP are symbols that perform operations on variables and values. PHP supports a wide variety of operators for tasks such as arithmetic, comparison, and logical operations. Understanding how to use operators correctly is key to writing efficient and readable PHP code. In this article, we'll cover some common interview questions and answers related to PHP operators.


What are operators in PHP?

Answer:
Operators are symbols that tell the PHP engine to perform specific operations on variables or values. Operators are used in expressions to perform tasks such as addition, subtraction, comparison, and logical evaluation.


What are the different types of operators in PHP?

Answer:
PHP supports several types of operators:

  • Arithmetic Operators: Perform mathematical operations.
  • Assignment Operators: Assign values to variables.
  • Comparison Operators: Compare two values.
  • Logical (Boolean) Operators: Combine multiple conditions.
  • Increment/Decrement Operators: Increase or decrease a variable's value.
  • String Operators: Perform operations on strings.
  • Array Operators: Perform operations on arrays.
  • Ternary and Null Coalescing Operators: Conditional operators.
  • Bitwise Operators: Perform operations at the binary level.

What are arithmetic operators in PHP?

Answer:
Arithmetic operators in PHP are used to perform basic mathematical operations on numeric values.

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus, remainder of division)
  • ** (Exponentiation)

Example:

$x = 10;
$y = 3;
echo $x + $y; 
// Output: 13

echo $x % $y; 
// Output: 1 (remainder of 10 divided by 3)

What are assignment operators in PHP?

Answer:
Assignment operators are used to assign values to variables. The basic assignment operator is =, but there are also compound operators that combine assignment with other operations.

  • =: Assign value to a variable.
  • +=: Add and assign the result.
  • -=: Subtract and assign the result.
  • *=: Multiply and assign the result.
  • /=: Divide and assign the result.
  • %=: Calculate modulus and assign the result.

Example:

$x = 10;
$x += 5; // Equivalent to $x = $x + 5; Output: 15

What are comparison operators in PHP?

Answer:
Comparison operators are used to compare two values. The result of a comparison is either true or false.

  • ==: Equal.
  • ===: Identical (equal and of the same type).
  • != or <>: Not equal.
  • !==: Not identical.
  • <: Less than.
  • >: Greater than.
  • <=: Less than or equal to.
  • >=: Greater than or equal to.

Example:

$x = 5;
$y = "5";
var_dump($x == $y);  // true, because values are equal
var_dump($x === $y); // false, because types are different (int vs string)

What are logical operators in PHP?

Answer:
Logical operators are used to combine multiple conditions in expressions.

  • && (AND): Returns true if both operands are true.
  • || (OR): Returns true if either operand is true.
  • ! (NOT): Inverts the value of a boolean.
  • and, or: These are lower-precedence versions of && and ||.

Example:

$x = true;
$y = false;
var_dump($x && $y); // false, because both conditions are not true
var_dump($x || $y); // true, because one condition is true

What are increment and decrement operators in PHP?

Answer:
Increment and decrement operators are used to increase or decrease the value of a variable by 1.

  • ++$var: Pre-increment (increments before the value is used).
  • $var++: Post-increment (increments after the value is used).
  • --$var: Pre-decrement (decrements before the value is used).
  • $var--: Post-decrement (decrements after the value is used).

Example:

$x = 5;
echo ++$x; // Output: 6 (pre-increment)
echo $x++; // Output: 6 (post-increment, increments after use)

What are string operators in PHP?

Answer:
PHP provides two string operators:

  • . (Concatenation): Joins two strings together.
  • .= (Concatenation assignment): Appends a string to an existing variable.

Example:

$str1 = "Hello";
$str2 = "World";
echo $str1 . " " . $str2; // Output: Hello World
$str1 .= " Everyone";
echo $str1; // Output: Hello Everyone

What is the ternary operator in PHP?

Answer:
The ternary operator is a shorthand for an if-else statement. It has the form condition ? trueResult : falseResult.

Example:

$age = 20;
echo ($age >= 18) ? "Adult" : "Minor"; // Output: Adult

What is the null coalescing operator (??) in PHP?

Answer:
The null coalescing operator is used to return the first operand if it exists and is not null; otherwise, it returns the second operand. It's commonly used to provide default values for variables.

$name = $_GET['name'] ?? 'Guest';
echo $name; // If 'name' is not set, it will output 'Guest'

What are bitwise operators in PHP?

Answer:
Bitwise operators perform operations on the binary representations of integers.

  • & (AND): Performs bitwise AND.
  • | (OR): Performs bitwise OR.
  • ^ (XOR): Performs bitwise XOR.
  • ~ (NOT): Performs bitwise NOT.
  • << (Shift Left): Shifts bits to the left.
  • >> (Shift Right): Shifts bits to the right.

Example:

$x = 6; // 110 in binary
$y = 3; // 011 in binary
echo $x & $y; // Output: 2 (010 in binary)

What is the spaceship operator (<=>) in PHP?

Answer:
The spaceship operator is used for three-way comparison. It returns:

  • -1 if the left operand is less than the right.
  • 0 if they are equal.
  • 1 if the left operand is greater than the right.

Example:

echo 5 <=> 10; // Output: -1
echo 10 <=> 10; // Output: 0
echo 15 <=> 10; // Output: 1

What are array operators in PHP?

Answer:
PHP provides operators to work with arrays.

  • + (Union): Combines two arrays.
  • ==: Returns true if two arrays have the same key-value pairs.
  • ===: Returns true if two arrays have the same key-value pairs in the same order and types.
  • != or <>: Returns true if two arrays are not equal.
  • !==: Returns true if two arrays are not identical.

Example:

$arr1 = ["a" => 1, "b" => 2];
$arr2 = ["b" => 2, "a" => 1];
var_dump($arr1 == $arr2);  // true (same key-value pairs)
var_dump($arr1 === $arr2); // false (order is different)

What are the precedence and associativity of operators in PHP?

Answer:
Operator precedence determines the order in which operators are evaluated. Associativity determines the direction in which expressions with the same precedence are evaluated (left-to-right or right-to-left).

Example:

$x = 10 + 3 * 5; // Multiplication has higher precedence than addition, so the result is 25.

In the case of the same precedence, associativity is applied:

$x = 10 - 5 + 2; // Left-to-right associativity, result is 7.

What is the purpose of using parentheses with operators in PHP?

Answer:
Parentheses can be used to explicitly define the order of operations in an expression, overriding the default precedence.

$x = (10 + 3) * 5; // Parentheses change the order, result is 65.
Ads