1. What does PHP stand for?
- Personal Home Page
- Private Hypertext Processor
- PHP: Hypertext Preprocessor
- Public Hosting Program
Correct Answer: PHP: Hypertext Preprocessor
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
PHP · Complete Question Bank
Practice the complete collection of PHP interview questions including theory, coding, MCQs and real interview problems.
Questions
220
Full database
Topics
67
Categorised
Difficulty
Mixed Levels
Easy Hard
Scroll through every important PHP question asked in real interviews. Includes MCQs, subjective questions, and coding prompts.
Correct Answer: PHP: Hypertext Preprocessor
Correct Answer: $
Browse more PHP questions, explore related subjects, or practice full interview sets to strengthen your preparation.
$name = 'John';
Correct Answer: Both A and B
// comment or # comment
Correct Answer: echo
echo 'Hello World';
Correct Answer: Character
Correct Answer: Yes
Correct Answer: define()
define('PI', 3.14);Correct Answer: 105
$result = 10 . 5; // gives 105
Correct Answer: $GLOBALS
<?php echo 'Hello'; ?>
$num = (int)'25';
echo 'Hi'; print 'Hello';
Correct Answer: Both B and C
if ($x > 10) { echo 'Big'; }
// or
if ($x > 10): echo 'Big'; endif;Correct Answer: break
switch($day){ case 'Mon': echo 'Work'; break; }Correct Answer: Three
for($i=0; $i<5; $i++){ echo $i; }Correct Answer: while
while($x < 5){ echo $x; $x++; }Correct Answer: do-while executes at least once
do { echo $x; $x++; } while ($x < 5);Correct Answer: foreach
foreach($arr as $val){ echo $val; }Correct Answer: function
function greet(){ echo 'Hello'; }Correct Answer: It returns null
function test(){} $x = test(); // nullCorrect Answer: Using assign =
function greet($name='User'){ echo $name; }for($i=0;$i<3;$i++){ for($j=0;$j<3;$j++){ echo $i.$j; } }function show(){ global $x; echo $x; }function fact($n){ if($n==1) return 1; return $n*fact($n-1); }$add = function($a,$b){ return $a+$b; }; echo $add(2,3);function addOne(&$num){ $num++; } $x=5; addOne($x);Correct Answer: Indexed, Associative, Multidimensional
$arr = array('a','b'); $map = array('name'=>'John');Correct Answer: Both A and B
$a = array(1,2,3); $b = [1,2,3];
Correct Answer: array_merge()
$result = array_merge($a,$b);
Correct Answer: count()
count($arr);
Correct Answer: Checks if a value exists in an array
in_array('apple', $fruits);Correct Answer: implode() and explode()
$s = implode(',', $arr); $a = explode(',', $s);Correct Answer: array_key_exists()
array_key_exists('name', $user);Correct Answer: strlen()
strlen('Hello');Correct Answer: substr()
substr('Hello',1,3); // ell$user = array('name'=>'John','age'=>25);$matrix = [[1,2,3],[4,5,6]]; echo $matrix[1][2];
strpos('hello world','world');foreach($arr as $key=>$val){ echo $key.'='.$val; }$name='John'; echo "Hello $name"; // prints Hello John
Correct Answer: $_DATA
Correct Answer: GET
Correct Answer: $_POST
Correct Answer: $_SERVER
echo $_SERVER['PHP_SELF'];
Correct Answer: Both GET and POST data
Correct Answer: $_FILES
echo $_FILES['myfile']['name'];
Correct Answer: setcookie()
setcookie('user','John',time()+3600);Correct Answer: session_start()
session_start(); $_SESSION['user']='John';
Correct Answer: setcookie('name','')
setcookie('user','',time()-3600);if(empty($_POST['name'])) echo 'Name required';
session_unset(); session_destroy();
// Generate token $_SESSION['token']=bin2hex(random_bytes(16));
Correct Answer: class
class Car { public $brand; }
$car = new Car();Correct Answer: __construct()
class Car {
function __construct(){ echo 'Object created'; }
}Correct Answer: private, protected, public
class Test { public $a; protected $b; private $c; }Correct Answer: extends
class Vehicle {}
class Car extends Vehicle {}Correct Answer: Redefining a parent class method in a child class
class ParentClass { function greet(){ echo 'Hi'; } }
class Child extends ParentClass { function greet(){ echo 'Hello'; } }Correct Answer: static
class Math { static $pi=3.14; static function add($a,$b){ return $a+$b; } }
echo Math::$pi;Correct Answer: The current object
class User { public $name; function say(){ echo $this->name; } }Correct Answer: interface
interface Animal { public function sound(); }
class Dog implements Animal { public function sound(){ echo 'Bark'; } }Correct Answer: Multiple inheritance
trait Logger { function log(){ echo 'Logging...'; } }
class App { use Logger; }abstract class Shape { abstract function area(); }interface Shape { function draw(); }class Demo { function __construct(){} function __destruct(){} }class BankAccount { private $balance; }$obj2 = clone $obj1;
class Base { static function who(){} static function call(){ static::who(); } }Correct Answer: Null Error
Correct Answer: error_reporting()
error_reporting(E_ALL);
Correct Answer: try-catch
try { throw new Exception('Error'); } catch(Exception $e) { echo $e->getMessage(); }Correct Answer: throw
throw new Exception('Invalid input');Correct Answer: Exception
class MyError extends Exception {}Correct Answer: To execute cleanup code always
try { } catch(Exception $e){ } finally { echo 'Done'; }Correct Answer: Extend the Exception class
class MyException extends Exception { }Correct Answer: error_log()
error_log('Database connection failed');Correct Answer: trigger_error()
trigger_error('Invalid input', E_USER_WARNING);set_error_handler('handlerFunction');class MyException extends Exception {}$e->getMessage(); $e->getFile();
Correct Answer: fopen()
fopen('data.txt','r');Correct Answer: Append only
fopen('log.txt','a');Correct Answer: fgets()
$line = fgets($handle);
Correct Answer: fwrite()
fwrite($handle,'Hello');
Correct Answer: fclose()
fclose($handle);
Correct Answer: file_get_contents()
$data = file_get_contents('info.txt');Correct Answer: file_put_contents()
file_put_contents('info.txt','Hello World');Correct Answer: file_exists()
if(file_exists('data.txt')) { echo 'Found'; }Correct Answer: unlink()
unlink('old.txt');fseek($handle,0);
fread($handle, filesize('data.txt'));move_uploaded_file($_FILES['photo']['tmp_name'],'uploads/photo.jpg');
mkdir('uploads');Correct Answer: MySQLi
$conn = mysqli_connect('localhost','root','pass','test');Correct Answer: PHP Data Object
$pdo = new PDO('mysql:host=localhost;dbname=test','root','pass');Correct Answer: Returns false
if(!$conn){ echo mysqli_connect_error(); }Correct Answer: mysqli_query()
$result = mysqli_query($conn,'SELECT * FROM users');
Correct Answer: mysqli_fetch_assoc()
$row = mysqli_fetch_assoc($result);
Correct Answer: Both A and B
$stmt = $pdo->prepare('SELECT * FROM users WHERE id=?');Correct Answer: bind_param()
$stmt->bind_param('s',$name);Correct Answer: fetch()
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$pdo->beginTransaction(); $pdo->commit();
Correct Answer: session_start()
session_start();
Correct Answer: $_SESSION
$_SESSION['user'] = 'John';
Correct Answer: $_COOKIE
echo $_COOKIE['user'];
Correct Answer: session_destroy()
session_destroy();
Correct Answer: In temporary files on server
Correct Answer: It deletes immediately
setcookie('user','',time()-3600);Correct Answer: Verify user identity
Correct Answer: Representational State Transfer
Correct Answer: json_encode()
json_encode($data);
Correct Answer: Decodes a JSON string
$arr = json_decode($json, true);
Correct Answer: PUT
Correct Answer: 404
Correct Answer: Making HTTP requests
$ch = curl_init('https://api.example.com');Correct Answer: Sends a new HTTP header to the browser
header('Location: login.php');Correct Answer: Content-Type
header('Content-Type: application/json');Correct Answer: Avoid name conflicts
namespace MyApp\Controllers;
Correct Answer: autoloading
spl_autoload_register(function($class){ include $class.'.php'; });Correct Answer: A dependency manager
composer install
Correct Answer: Store compiled scripts in memory
Correct Answer: memory_limit
ini_set('memory_limit','256M');Correct Answer: Reclaims memory from unused objects
gc_collect_cycles();
Correct Answer: Temporarily holding script output
ob_start(); echo 'Hello'; ob_end_flush();
Correct Answer: Xdebug
Correct Answer: Just in Time
Correct Answer: Union types
function sum(int|float $a, int|float $b){ return $a+$b; }Correct Answer: match
echo match($status){ 200 => 'OK', 404 => 'Not Found', default => 'Error' };Correct Answer: Call functions with unordered parameters
displayUser(age:25, name:'John');
Correct Answer: Prevents errors when accessing null objects
$user?->getProfile()?->getName();
Correct Answer: Constructor property promotion
class User { function __construct(public string $name, private int $age){} }Correct Answer: Adding metadata to code elements
#[Route('/home')] class HomeController {}Correct Answer: Any type of value
function processData(mixed $data){ return $data; }Correct Answer: str_contains()
str_contains('Hello World','World');Correct Answer: Associating data with objects without preventing garbage collection
$map = new WeakMap();
Correct Answer: Web application development
Correct Answer: Model View Controller
Correct Answer: A CLI tool
php artisan make:controller UserController
Correct Answer: routes/web.php
Route::get('/home', [HomeController::class, 'index']);Correct Answer: An ORM
User::where('active',1)->get();Correct Answer: Managing database schema
php artisan make:migration create_users_table
Correct Answer: A templating engine
@if($user) Welcome, {{ $user->name }} @endifCorrect Answer: A request filter
php artisan make:middleware CheckUser
Correct Answer: The central place for app bootstrapping
Correct Answer: strrev()
echo strrev('Hello'); // olleHCorrect Answer: count()
count([1,2,3,4]); // 4
Correct Answer: A string that reads the same backward and forward
Correct Answer: 120
Correct Answer: 8
Correct Answer: sort()
sort($arr);
Correct Answer: %
if($num % 2 == 0) echo 'Even';
Correct Answer: array_sum()
array_sum([2,4,6]); // 12
$words = explode(' ', $str); echo implode(' ', array_reverse($words));$unique = array_unique($arr);
str_word_count('Hello world!');max($arr); min($arr);
if($str == strrev($str)) echo 'Palindrome';
array_diff($arr1,$arr2);
Correct Answer: Avoid XSS attacks
echo htmlspecialchars($input, ENT_QUOTES);
Correct Answer: Use CSRF tokens
Correct Answer: Use prepared statements
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');Correct Answer: password_hash()
password_hash('mypassword', PASSWORD_DEFAULT);Correct Answer: Use HTTPS and regenerate session ID
session_regenerate_id(true);
Correct Answer: Log errors and hide from users
ini_set('display_errors',0); ini_set('log_errors',1);Correct Answer: .env file
Correct Answer: Identify and authorize requests