PHP Operators Interview Questions & Answers

PHP operators interview questions: == vs ===, != vs !==, division vs modulus, arithmetic operators, ternary operator, null coalescing operator (??) and string concatenation.

How will you concatenate two strings in PHP?

To join (concatenate) two strings in PHP, use the dot operator (.). To add text at the end of an existing variable, use .=.

<?php
$first = "Hello";
$second = "World";

echo $first . " " . $second . "<br>";

$text = "Hello";
$text .= " PHP";
echo $text . "<br>";

echo "$first $second";
?>

Output:

Tutorials Class - Output Window

Hello World
Hello PHP
Hello World

The last line puts the variables inside double quotes, which also works. Remember that the plus sign (+) does not join strings in PHP like in JavaScript, it tries to add numbers. Read more in PHP String Handling.

What is the difference between == and === Operators in PHP?

== (equal) compares only the values, and PHP may change the data type before comparing. === (identical) compares the value and the data type, so it is strict.

<?php
$num_1 = 12;      // integer
$num_2 = "12";    // string

var_dump($num_1 == $num_2);
var_dump($num_1 === $num_2);
var_dump(0 == "abc");
var_dump(null == false);
var_dump(null === false);
?>

Output:

Output in PHP 8

bool(true)
bool(false)
bool(false)
bool(true)
bool(false)

  • The first comparison is true because the values are same, but the second is false because one is an integer and the other is a string.
  • 0 == "abc" was true in PHP 7, but PHP 8 changed it to false.
  • null == false is true, because both are false-like. With === it is false.

Use === in your conditions. The opposite operators are != and !==, see != vs !==. All operators are in PHP Operators.

What are the arithmetic operators in PHP?

Arithmetic operators are used to do maths on numbers. PHP has these arithmetic operators:

OperatorNameExampleResult
+Addition10 + 313
–Subtraction10 – 37
*Multiplication10 * 330
/Division10 / 33.3333333333333
%Modulus (remainder)10 % 31
**Exponentiation (power)10 ** 31000
<?php
$a = 10;
$b = 3;

echo ($a + $b) . "<br>";
echo ($a - $b) . "<br>";
echo ($a * $b) . "<br>";
echo ($a / $b) . "<br>";
echo ($a % $b) . "<br>";
echo ($a ** $b);
?>

Output:

Tutorials Class - Output Window

13
7
30
3.3333333333333
1
1000

Division always gives a float when the result is not a whole number. Use intdiv() if you need only the whole part. Read all operators in PHP Operators.

What is the use of the ternary operator in PHP?

The ternary operator is a short way to write a simple if-else statement in one line. The syntax is condition ? value_if_true : value_if_false.

<?php
$age = 20;

$result = ($age >= 18) ? "Adult" : "Minor";

echo $result;
?>

Output:

Tutorials Class - Output Window

Adult

There is also a shorter form ?: (called Elvis operator). It returns the first value if it is not empty, otherwise the second one.

<?php
$username = "";

echo $username ?: "Guest";
?>

Output:

Tutorials Class - Output Window

Guest

Use it only for short and simple conditions. For long conditions a normal if-else is easier to read. More details in PHP Ternary Operator.

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

The null coalescing operator (??) returns the left value if it exists and is not NULL, otherwise it returns the right value. It is a short form of isset() with a ternary operator, and it never gives an “undefined” warning.

<?php
$user = ["name" => "Robin"];

echo $user["city"] ?? "Not given";
echo "<br>";
echo $user["name"] ?? "Not given";
?>

Output:

Tutorials Class - Output Window

Not given
Robin

From PHP 7.4 you can also use ??=. It sets the value only when the variable is not set or is NULL.

<?php
$settings = [];

$settings["theme"] ??= "light";

echo $settings["theme"];
?>

Output:

Tutorials Class - Output Window

light

It is very useful with $_GET and $_POST, for example $page = $_GET["page"] ?? 1;. See also PHP isset().

What is the difference between division (/) and modulus (%) in PHP?

The division operator (/) gives the result of dividing one number by another. The modulus operator (%) gives only the remainder of that division.

<?php
$a = 10;
$b = 3;

echo $a / $b . "<br>";        // division
echo $a % $b . "<br>";        // modulus (remainder)
echo intdiv($a, $b);          // whole number part only
?>

Output:

Tutorials Class - Output Window

3.3333333333333
1
3

Modulus is often used to check even or odd numbers. If $number % 2 is 0, the number is even.

Note: In PHP 8, dividing or using modulus with zero throws a DivisionByZeroError. Check the divisor before the calculation.

What is the difference between != and !== in PHP?

!= (not equal) checks only the value. !== (not identical) checks the value and the data type. So !== is more strict.

<?php
$a = 5;       // integer
$b = "5";     // string

var_dump($a != $b);    // values are same, so false
var_dump($a !== $b);   // data types are different, so true
?>

Output:

Tutorials Class - Output Window

bool(false)
bool(true)

In PHP, <> works same as !=. The same logic applies to the opposite operators, see difference between == and ===.

What is the difference between $var++ and $var + 1 in PHP?

$var + 1 only calculates a new value and does not change $var. $var++ is the increment operator, it changes the value of $var by adding 1.

There is one more thing to remember. $var++ (post-increment) returns the old value first and then increases. ++$var (pre-increment) increases first and then returns the new value.

<?php
$a = 5;
$b = $a + 1;      // $a is not changed
echo $a . " " . $b . "<br>";

$x = 5;
$y = $x++;        // $y gets old value 5, then $x becomes 6
echo $x . " " . $y . "<br>";

$m = 5;
$n = ++$m;        // $m becomes 6 first, then $n gets 6
echo $m . " " . $n;
?>

Output:

Tutorials Class - Output Window

5 6
6 5
6 6