Free practice questions

Technical
Interview questions

Prepare for technical interviews with questions by technology, clear explanations, step-by-step solutions and code you can run.

PHP questions
50 questions
25
25 concept questions
25
25 coding questions

PHP technical questions

PHP 8.x Architecture, OOP, PDO Database, Security (XSS/CSRF/SQLi), Sessions & Coding Challenges
Total 50 Questions
Part A: Core concept & theory questions
Questions 01 to 25 • Theoretical Foundations
25 Concept Questions

PHP is a language that runs on the server and mostly builds web pages. You write the code, the server runs it, and the browser only gets the finished HTML. Nobody can view your PHP from the page source.

The browser asks for the page, Apache or Nginx passes the file to PHP, PHP runs it and creates the output, and the server sends that back. When the request is over, everything in memory is cleared.

JavaScript in the browser is client-side, and the user can see and change it. PHP is server-side, so it's where I keep database logic and anything private.

== only checks the value, so "5" == 5 is true. === checks the type too, so that's false. I stick with === because it saves me from weird bugs.

Both print things. echo accepts multiple values and is a tiny bit faster. print takes just one and returns 1, which almost nobody uses.

If the file is missing, include gives a warning and moves on, while require kills the script. The _once versions load the file only one time, so nothing gets declared twice.

GET puts the data in the URL, so anyone can see it and it has a size limit. POST sends it in the request body, so it's the one for login forms and uploads.

A cookie sits in the user's browser. A session sits on the server, and the browser only holds an ID. I keep login info in sessions because they're harder to tamper with.

int, float, string, bool, array, object, null and resource.

PHP converts types on its own. "10" + 5 gives 15 without complaint. It's handy, but it can hide mistakes, so I turn on strict types in bigger projects.

Think of it as a folder for classes. Two libraries can both have a User class and not clash, because each one lives in its own namespace.

A trait is a bundle of methods that a class can borrow with use. PHP allows only one parent class, so traits are how I share code between classes that aren't related.

An abstract class can have real code, but a class extends only one. An interface is just a list of methods a class promises to have, and a class can implement several.

They're the ones starting with __, like __construct and __toString. I never call them myself, because PHP does at the right moment, for example when an object is created or printed.

It belongs to the class instead of one object. I can call Helper::format() without doing new Helper. It's good for utilities and counters.

A variable made inside a function stays inside it. To use an outside one, I have to pass it in or use global. A static variable inside a function remembers its value between calls.

MySQLi works only with MySQL. PDO works with many databases, so if I switch later, I change very little. I use PDO because the code looks cleaner.

It's when someone types SQL into a form field and my query runs it. The fix is prepared statements, so the input is treated as data and never as part of the query.

It's the package manager for PHP, like npm for JavaScript. I list libraries in composer.json, and it downloads them and sets up autoloading.

Without it, passing "5" to a function that wants an int just works. With declare(strict_types=1);, PHP throws an error instead. It catches sloppy mistakes early.

self means the class where the code is written. static means the class that was actually called at runtime, which matters when child classes are involved.

It says "use this value, but if it's missing or null, use that one." So $_GET['page'] ?? 1 replaces a long isset check.

Risky code goes in try. If it throws, PHP jumps to catch. The finally block runs no matter what, which is good for closing files or connections.

Never as plain text, and not with MD5 either. I use password_hash() to save and password_verify() to check at login.

They're agreed rules from the PHP community so everyone writes similar-looking code. PSR-12 is for style and PSR-4 is for autoloading.

Part B: Coding questions & algorithm solutions
Questions 01 to 25 • Runnable Solution Scripts
25 Coding Questions

Check remainder when dividing by 2 using ternary operator.

PHP Solution
$n = 7;
echo ($n % 2 == 0) ? "Even" : "Odd";

Use arithmetic addition and subtraction to swap values in place.

PHP Solution
$a = 5; $b = 9;
$a = $a + $b;   // 14
$b = $a - $b;   // 5
$a = $a - $b;   // 9
echo "$a $b";

Extract last digit with modulo 10 and truncate using integer division intdiv().

PHP Solution
$n = 4582; $sum = 0;
while ($n > 0) {
    $sum += $n % 10;     // grab the last digit
    $n = intdiv($n, 10); // drop it
}
echo $sum; // 19

Multiply current reverse by 10 and add last digit in a loop.

PHP Solution
$n = 1234; $rev = 0;
while ($n > 0) {
    $rev = $rev * 10 + $n % 10;
    $n = intdiv($n, 10);
}
echo $rev; // 4321

Check if sum of cubes of individual digits equals original number.

PHP Solution
$n = 153; $t = $n; $sum = 0;
while ($t > 0) {
    $d = $t % 10;
    $sum += $d ** 3;
    $t = intdiv($t, 10);
}
echo ($sum == $n) ? "Armstrong" : "Not Armstrong";

Iterate backwards from string length minus one.

PHP Solution
$s = "laravel"; $r = "";
for ($i = strlen($s) - 1; $i >= 0; $i--) {
    $r .= $s[$i];
}
echo $r;

Sanitize non-alphanumeric characters, convert to lowercase, and compare with strrev().

PHP Solution
function isPal(string $s): bool {
    $clean = strtolower(preg_replace('/[^a-z0-9]/i', '', $s));
    return $clean === strrev($clean);
}
var_dump(isPal("Nitin")); // true

Base case returning 1 when n <= 1, otherwise multiplying n by fact(n-1).

PHP Solution
function fact(int $n): int {
    return $n <= 1 ? 1 : $n * fact($n - 1);
}
echo fact(6); // 720

Iteratively update pointers \$a and \$b to generate series.

PHP Solution
$a = 0; $b = 1;
for ($i = 0; $i < 10; $i++) {
    echo $a . " ";
    $next = $a + $b;
    $a = $b;
    $b = $next;
}

Check divisibility up to square root sqrt(n).

PHP Solution
function isPrime(int $n): bool {
    if ($n < 2) return false;
    for ($i = 2; $i * $i <= $n; $i++) {
        if ($n % $i == 0) return false;
    }
    return true;
}

Loop 1 to 50 and filter using prime checker helper.

PHP Solution
for ($i = 1; $i <= 50; $i++) {
    if (isPrime($i)) echo $i . " ";
}

Iterate through array tracking current maximum value.

PHP Solution
$nums = [12, 45, 7, 89, 23];
$big = $nums[0];
foreach ($nums as $n) {
    if ($n > $big) $big = $n;
}
echo $big; // 89

Remove duplicates with array_unique(), sort descending with rsort(), and pick index 1.

PHP Solution
$nums = [12, 45, 7, 89, 23];
$u = array_unique($nums);
rsort($u);
echo $u[1]; // 45

Repeatedly swap adjacent elements if out of order.

PHP Solution
$a = [5, 2, 9, 1, 7];
$n = count($a);
for ($i = 0; $i < $n - 1; $i++) {
    for ($j = 0; $j < $n - $i - 1; $j++) {
        if ($a[$j] > $a[$j + 1]) {
            $tmp = $a[$j];
            $a[$j] = $a[$j + 1];
            $a[$j + 1] = $tmp;
        }
    }
}
print_r($a);

Use array_unique() and re-index with array_values().

PHP Solution
$data = [3, 1, 3, 2, 1];
print_r(array_values(array_unique($data)));

Extract words via str_word_count() and count occurrences with array_count_values().

PHP Solution
$text = "php is easy and php is fun";
print_r(array_count_values(str_word_count($text, 1)));

Use regex matching preg_match_all() with vowel character class.

PHP Solution
echo preg_match_all('/[aeiou]/i', "Programming"); // 3

Use ucwords() to capitalize first letter of each word in string.

PHP Solution
echo ucwords("learning php is fun");

Loop 1 through 10 and format product output.

PHP Solution
$n = 7;
for ($i = 1; $i <= 10; $i++) {
    echo "$n x $i = " . ($n * $i) . "<br>";
}

Use str_repeat() for non-breaking space padding and asterisk printing.

PHP Solution
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
    echo str_repeat("&nbsp;", $rows - $i);
    echo str_repeat("* ", $i) . "<br>";
}

Split string into character arrays using str_split(), sort both with sort(), and compare equality.

PHP Solution
function isAnagram($a, $b) {
    $x = str_split(strtolower($a)); sort($x);
    $y = str_split(strtolower($b)); sort($y);
    return $x === $y;
}
var_dump(isAnagram("listen", "silent")); // true

Divide sorted search space using two pointers and mid point calculation.

PHP Solution
function bsearch(array $arr, int $target): int {
    $low = 0; $high = count($arr) - 1;
    while ($low <= $high) {
        $mid = intdiv($low + $high, 2);
        if ($arr[$mid] == $target) return $mid;
        if ($arr[$mid] < $target) $low = $mid + 1;
        else $high = $mid - 1;
    }
    return -1; // not found
}

Combine multiple arrays using array_merge().

PHP Solution
print_r(array_merge([1, 2], [3, 4]));

Traverse arbitrarily nested arrays using array_walk_recursive() with reference accumulator.

PHP Solution
$nested = [1, [2, 3], [4, [5]]];
$flat = [];
array_walk_recursive($nested, function ($v) use (&$flat) {
    $flat[] = $v;
});
print_r($flat);

Subtract array_sum() from expected sum n*(n+1)/2.

PHP Solution
$arr = [1, 2, 4, 5]; $n = 5;
$expected = $n * ($n + 1) / 2;
echo $expected - array_sum($arr); // 3

No Matching Questions Found

Try searching for keywords like "JWT", "Express", "useEffect", "PDO", "GIL", or "schema".

Technical Panel Preparation

Ready to Benchmark Your Technical Skills?

Connect with DigitalXNode expert interviewers for technical mock interviews, domain assessment, and enterprise job placement.

1