1. Home
  2. Docs
  3. PHP
  4. Interview Questions
  5. PHP Technical Test Revision Guide

PHP Technical Test Revision Guide

Below is a comprehensive PHP technical test revision guide with explanations and code snippets covering:

  • Syntax & fundamentals
  • Control structures (if, switch, loops)
  • Functions
  • Arrays (indexed & associative)
  • String manipulation
  • Form handling
  • Sessions & cookies
  • File handling
  • OOP (classes, interfaces, traits, inheritance)
  • Error handling
  • Database (PDO + prepared statements)
  • Security basics
  • Common interview logic challenges
  • Mini practice problems

This will simulate the kinds of tasks typically given in PHP interview technical tests.

1️⃣ PHP Fundamentals

Basic PHP Structure

<?php
echo "Hello, World!";
?>

Variables

<?php
$name = "John";
$age = 30;
$height = 5.9;
$isActive = true;

echo "Name: " . $name;
?>

Constants

<?php
define("SITE_NAME", "My Website");
echo SITE_NAME;
?>

2️⃣ Conditional Statements

If / Else

<?php
$score = 75;

if ($score >= 90) {
    echo "A Grade";
} elseif ($score >= 75) {
    echo "B Grade";
} else {
    echo "Needs Improvement";
}
?>

Ternary Operator

<?php
$age = 18;
$message = ($age >= 18) ? "Adult" : "Minor";
echo $message;
?>

Switch

<?php
$day = "Monday";

switch ($day) {
    case "Monday":
        echo "Start of week";
        break;
    case "Friday":
        echo "Almost weekend";
        break;
    default:
        echo "Midweek";
}
?>

3️⃣ Loops

For Loop

<?php
for ($i = 1; $i <= 10; $i++) {
    echo $i . "<br>";
}
?>

While Loop

<?php
$count = 1;

while ($count <= 5) {
    echo $count . "<br>";
    $count++;
}
?>

Do-While Loop

<?php
$x = 1;

do {
    echo $x . "<br>";
    $x++;
} while ($x <= 3);
?>

Foreach (Arrays)

<?php
$fruits = ["Apple", "Banana", "Orange"];

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}
?>

Foreach with key:

<?php
$ages = [
    "John" => 25,
    "Jane" => 30
];

foreach ($ages as $name => $age) {
    echo "$name is $age years old<br>";
}
?>

4️⃣ Arrays

Indexed Array

<?php
$numbers = [10, 20, 30];
echo $numbers[0];
?>

Associative Array

<?php
$user = [
    "name" => "Alice",
    "email" => "alice@example.com"
];

echo $user["email"];
?>

Multidimensional Array

<?php
$users = [
    ["name" => "John", "age" => 25],
    ["name" => "Jane", "age" => 28]
];

echo $users[1]["name"];
?>

Common Array Functions

<?php
$numbers = [5, 2, 9, 1];

sort($numbers);
print_r($numbers);

array_push($numbers, 100);

$count = count($numbers);

$max = max($numbers);
$min = min($numbers);
?>

5️⃣ Functions

Basic Function

<?php
function greet($name) {
    return "Hello " . $name;
}

echo greet("John");
?>

Default Parameters

<?php
function multiply($a, $b = 2) {
    return $a * $b;
}

echo multiply(5); // 10
?>

Type Declarations (Modern PHP)

<?php
function add(int $a, int $b): int {
    return $a + $b;
}

echo add(3, 5);
?>

Anonymous Function

<?php
$square = function($n) {
    return $n * $n;
};

echo $square(4);
?>

6️⃣ String Manipulation

<?php
$text = " Hello World ";

echo trim($text);
echo strlen($text);
echo strtoupper($text);
echo strtolower($text);
echo str_replace("World", "PHP", $text);
?>

Explode & Implode

<?php
$csv = "apple,banana,orange";
$array = explode(",", $csv);

$newString = implode(" | ", $array);
echo $newString;
?>

7️⃣ Form Handling

HTML:

<form method="POST">
    <input type="text" name="username">
    <button type="submit">Submit</button>
</form>

PHP:

<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $username = htmlspecialchars($_POST["username"]);
    echo "Hello " . $username;
}
?>

8️⃣ Sessions & Cookies

<?php
session_start();

$_SESSION["user"] = "John";

echo $_SESSION["user"];
?>

Cookie

<?php
setcookie("username", "John", time() + 3600);

if (isset($_COOKIE["username"])) {
    echo $_COOKIE["username"];
}
?>

9️⃣ File Handling

Write to File

<?php
$file = fopen("test.txt", "w");
fwrite($file, "Hello File");
fclose($file);
?>

Read File

<?php
$content = file_get_contents("test.txt");
echo $content;
?>

🔟 Object-Oriented PHP

Class & Object

<?php
class Person {
    public $name;
    public $age;

    public function greet() {
        return "Hello, my name is " . $this->name;
    }
}

$person = new Person();
$person->name = "John";
echo $person->greet();
?>

Constructor

<?php
class Car {
    private $brand;

    public function __construct($brand) {
        $this->brand = $brand;
    }

    public function getBrand() {
        return $this->brand;
    }
}

$car = new Car("BMW");
echo $car->getBrand();
?>

Inheritance

<?php
class Animal {
    public function speak() {
        return "Animal sound";
    }
}

class Dog extends Animal {
    public function speak() {
        return "Bark";
    }
}

$dog = new Dog();
echo $dog->speak();
?>

Interface

<?php
interface Shape {
    public function area();
}

class Circle implements Shape {
    private $radius;

    public function __construct($radius) {
        $this->radius = $radius;
    }

    public function area() {
        return pi() * $this->radius * $this->radius;
    }
}
?>

1️⃣1️⃣ Error Handling

<?php
try {
    if (!file_exists("file.txt")) {
        throw new Exception("File not found");
    }
} catch (Exception $e) {
    echo $e->getMessage();
}
?>

1️⃣2️⃣ Database (PDO – VERY COMMON IN TESTS)

<?php
$pdo = new PDO("mysql:host=localhost;dbname=testdb", "root", "");

$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(":id", $id);
$id = 1;
$stmt->execute();

$result = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);
?>

Insert Example

<?php
$stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->execute(["John", "john@example.com"]);
?>

1️⃣3️⃣ Common Interview Logic Questions

Reverse a String

<?php
function reverseString($str) {
    return strrev($str);
}

echo reverseString("hello");
?>

Check Palindrome

<?php
function isPalindrome($str) {
    $clean = strtolower(str_replace(" ", "", $str));
    return $clean === strrev($clean);
}
?>

Fibonacci

<?php
function fibonacci($n) {
    if ($n <= 1) return $n;
    return fibonacci($n - 1) + fibonacci($n - 2);
}
?>

Find Largest in Array

<?php
function findMax($array) {
    $max = $array[0];
    foreach ($array as $value) {
        if ($value > $max) {
            $max = $value;
        }
    }
    return $max;
}
?>

1️⃣4️⃣ Security Essentials (Often Asked)

Password Hashing

<?php
$password = "mypassword";
$hash = password_hash($password, PASSWORD_DEFAULT);

if (password_verify("mypassword", $hash)) {
    echo "Password is valid";
}
?>

Prevent SQL Injection

Use prepared statements (shown earlier).

Prevent XSS

echo htmlspecialchars($userInput);

1️⃣5️⃣ Mini Practice Technical Test

Here’s a realistic interview-style problem:

Problem:

Create a function that:

  • Takes an array of numbers
  • Removes duplicates
  • Sorts ascending
  • Returns only even numbers
<?php
function processNumbers($numbers) {
    $unique = array_unique($numbers);
    sort($unique);

    $result = [];

    foreach ($unique as $num) {
        if ($num % 2 === 0) {
            $result[] = $num;
        }
    }

    return $result;
}

print_r(processNumbers([4,2,2,7,8,4,10,3]));
?>

What They May Actually Test

Based on typical PHP technical tests, expect:

  • Debug broken code
  • Write a function from scratch
  • Connect to a DB
  • Build simple CRUD
  • Manipulate arrays
  • Validate form input
  • Explain OOP concepts
  • Secure login system

How You Should Practise (Serious Advice)

Focus on:

  1. Writing logic cleanly
  2. Avoiding global variables
  3. Using functions properly
  4. Writing secure database queries
  5. Understanding OOP structure

How can we help?