PHP Interview Questions for Freshers

Top PHP Interview Questions for Freshers 2025

This PHP Interview Questions for Freshers guide is like a cheat code for freshers and newbies who wanna crack PHP job interviews. It’s got all the important questions that companies ask beginners, stuff like how PHP works on the server, how to build websites, and easy coding problems that freshers usually face.

It starts with basic things like PHP tags and variables, then slowly moves to cooler stuff like functions, arrays, and databases. It even teaches how to connect PHP with HTML and MySQL, plus some common, and core php tasks. Every question has simple answers with easy examples, so even if you’re totally new, you’ll get it.

Oh, and it also tells you about the silly mistakes new php devs make in interviews and how to avoid them. Basically, this guide helps you practice the most asked php interview questions and land your first PHP role.

We also have an in-depth php interview guide, you can check it here: PHP Interview Questions and Answers PDF

Basic PHP Interview Questions and Answers for Freshers

Que 1. What is the difference between require_once and include_once in PHP?

Answer: Both require_once and include_once include a file only once to prevent duplicate inclusion, but require_once causes a fatal error and stops execution if the file is missing, while include_once issues a warning and continues.

Example:

require_once 'config.php';  // Fatal error if missing
include_once 'config.php';  // Warning if missing

Que 2. How do you declare a function with default parameters in PHP?

Answer: In PHP, functions can have default parameters by assigning a value in the function definition, used if no argument is provided.

Example:

function greet($name = "Guest") {
    return "Hello, $name!";
}
echo greet();  // Outputs: Hello, Guest!

Que 3. What is the print_r() function in PHP, and how is it used for debugging?

Answer: The print_r() function displays human-readable information about a variable, especially arrays, useful for debugging. It can return output if the second parameter is true.

Example:

$array = ["a" => 1, "b" => 2];
print_r($array);  // Outputs: Array ( [a] => 1 [b] => 2 )

Que 4. How do you use the trim() function in PHP?

Answer: The trim() function removes whitespace or specified characters from both ends of a string.

Example:

$string = "  Hello  ";
echo trim($string);  // Outputs: Hello

Que 5. What is the purpose of the $_POST superglobal in PHP?

Answer: The $_POST superglobal retrieves data sent to the server via the HTTP POST method, typically from HTML forms.

Example:

// Form: <form method="post">
echo $_POST['username'];  // Outputs: user input

Que 6. How do you create a multidimensional array in PHP?

Answer: A multidimensional array is an array containing other arrays, accessed using multiple indices.

Example:

$matrix = [
    [1, 2, 3],
    [4, 5, 6]
];
echo $matrix[0][1];  // Outputs: 2

Que 7. What is the die() function in PHP, and when is it used?

Answer: The die() function outputs a message and terminates the script, often used for error handling or debugging.

Example:

$conn = mysqli_connect("localhost", "user", "pass", "db");
if (!$conn) {
    die("Connection failed");
}

Que 8. How do you use the strlen() function in PHP?

Answer: The strlen() function returns the length of a string in bytes.

Example:

$string = "Hello";
echo strlen($string);  // Outputs: 5

Que 9. What is the difference between GET and POST methods in PHP?

Answer:

FeatureGETPOST
Data in URLVisible in query stringHidden in request body
Data SizeLimited (browser-dependent)Larger capacity
SecurityLess secureMore secure

Que 10. How do you use the array_push() function in PHP?

Answer: The array_push() function adds one or more elements to the end of an array.

Example:

$array = [1, 2];
array_push($array, 3, 4);
print_r($array);  // Outputs: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

Que 11. What is the purpose of the exit() function in PHP?

Answer: The exit() function terminates script execution, optionally outputting a message, similar to die().

Example:

exit("Script stopped");  // Outputs: Script stopped

Que 12. How do you check if a variable is set in PHP?

Answer: Use isset() to check if a variable exists and is not NULL, returning true or false.

Example:

$var = "test";
if (isset($var)) {
    echo "Variable is set";  // Outputs: Variable is set
}

Que 13. What is the array_pop() function in PHP?

Answer: The array_pop() function removes and returns the last element of an array.

Example:

$array = [1, 2, 3];
$last = array_pop($array);
echo $last;  // Outputs: 3

Que 14. How do you use the strtolower() function in PHP?

Answer: The strtolower() function converts all characters in a string to lowercase.

Example:

$string = "HELLO";
echo strtolower($string);  // Outputs: hello

Que 15. What is the purpose of the $_GET superglobal in PHP?

Answer: The $_GET superglobal retrieves data sent to the server via URL query strings.

Example:

// URL: ?id=1
echo $_GET['id'];  // Outputs: 1

Que 16. How do you define a global variable in PHP?

Answer: Global variables are defined outside functions or accessed inside functions using the global keyword or $GLOBALS array.

Example:

$globalVar = 10;
function test() {
    global $globalVar;
    echo $globalVar;  // Outputs: 10
}

Que 17. What is the array_shift() function in PHP?

Answer: The array_shift() function removes and returns the first element of an array, shifting indices down.

Example:

$array = [1, 2, 3];
$first = array_shift($array);
echo $first;  // Outputs: 1

Que 18. How do you use the strtoupper() function in PHP?

Answer: The strtoupper() function converts all characters in a string to uppercase.

Example:

$string = "hello";
echo strtoupper($string);  // Outputs: HELLO

Que 19. What is the purpose of the array_keys() function in PHP?

Answer: The array_keys() function returns an array of all keys in an associative or indexed array.

Example:

$array = ["name" => "Alice", "age" => 25];
$keys = array_keys($array);
print_r($keys);  // Outputs: Array ( [0] => name [1] => age )

Que 20. How do you use the time() function in PHP?

Answer: The time() function returns the current Unix timestamp (seconds since January 1, 1970).

Example:

echo time();  // Outputs: current timestamp

Que 21. What is the difference between break and continue in PHP loops?

Answer: break exits the entire loop, while continue skips the current iteration and proceeds to the next.

Example:

for ($i = 0; $i < 5; $i++) {
    if ($i == 3) continue;
    echo $i;  // Outputs: 0124
}

Que 22. How do you use the array_values() function in PHP?

Answer: The array_values() function returns an array with all values from an input array, reindexing numerically.

Example:

$array = ["a" => 1, "b" => 2];
$values = array_values($array);
print_r($values);  // Outputs: Array ( [0] => 1 [1] => 2 )

Que 23. What is the htmlentities() function in PHP, and how does it differ from htmlspecialchars()?

Answer: Both htmlentities() and htmlspecialchars() convert special characters to HTML entities to prevent XSS, but htmlentities() converts all applicable characters, while htmlspecialchars() handles only a subset (&, “, ‘, <, >).

Example:

$string = "<b>Bold</b>";
echo htmlentities($string);  // Outputs: &lt;b&gt;Bold&lt;/b&gt;

Que 24. How do you use the mysqli_fetch_assoc() function in PHP?

Answer: The mysqli_fetch_assoc() function fetches a row from a MySQL result set as an associative array.

Example:

$result = $conn->query("SELECT * FROM users");
$row = $result->fetch_assoc();
echo $row['name'];  // Outputs: name from row

Que 25. What is the purpose of the rand() function in PHP?

Answer: The rand() function generates a random integer within a specified range.

Example:

echo rand(1, 10);  // Outputs: random number between 1 and 10
PHP Interview Questions Freshers

Core PHP Interview Questions and Answers for Freshers

Que 26. What is the purpose of the array_slice() function in PHP?

Answer: The array_slice() function extracts a portion of an array, starting from a specified position and optionally with a defined length.

Example:

$array = ["a", "b", "c", "d"];
$slice = array_slice($array, 1, 2);
print_r($slice);  // Outputs: Array ( [0] => b [1] => c )

Que 27. How do you use the substr() function in PHP to extract part of a string?

Answer: The substr() function extracts a substring from a string, given a start position and optional length.

Example:

$string = "Hello World";
echo substr($string, 6, 5);  // Outputs: World

Que 28. What is the difference between array_merge() and the + operator for arrays in PHP?

Answer:

Featurearray_merge()+ Operator
Key HandlingOverwrites duplicate keysKeeps first occurrence
Numeric KeysReindexes if neededPreserves original keys
Multiple ArraysMerges multiple arraysOnly two arrays

Example:

$arr1 = ["a" => 1];
$arr2 = ["a" => 2];
print_r(array_merge($arr1, $arr2));  // Outputs: Array ( [a] => 2 )
print_r($arr1 + $arr2);  // Outputs: Array ( [a] => 1 )

Que 29. What is the purpose of the array_map() function in PHP?

Answer: The array_map() function applies a callback function to each element of one or more arrays, returning a new array with the results.

Example:

$array = [1, 2, 3];
$result = array_map(fn($n) => $n * 2, $array);
print_r($result);  // Outputs: Array ( [0] => 2 [1] => 4 [2] => 6 )

Que 30. How do you use the str_repeat() function in PHP?

Answer: The str_repeat() function repeats a string a specified number of times.

Example:

echo str_repeat("Hi ", 3);  // Outputs: Hi Hi Hi 

Que 31. What is the purpose of the array_filter() function in PHP?

Answer: The array_filter() function filters elements of an array using a callback function, returning a new array with elements that pass the test.

Example:

$array = [1, 2, 3, 4];
$evens = array_filter($array, fn($n) => $n % 2 == 0);
print_r($evens);  // Outputs: Array ( [1] => 2 [3] => 4 )

Que 32. How do you use the number_format() function in PHP?

Answer: The number_format() function formats a number with grouped thousands and optional decimal places.

Example:

$number = 1234.567;
echo number_format($number, 2);  // Outputs: 1,234.57

Que 33. What is the difference between include and include_once in PHP?

Answer: include loads a file and issues a warning if it’s missing, while include_once ensures the file is included only once, preventing redefinition errors.

Example:

include 'config.php';      // May include multiple times
include_once 'config.php'; // Includes only once

Que 34. How do you use the array_unique() function in PHP?

Answer: The array_unique() function removes duplicate values from an array, keeping the first occurrence.

Example:

$array = [1, 2, 2, 3];
$unique = array_unique($array);
print_r($unique);  // Outputs: Array ( [0] => 1 [1] => 2 [2] => 3 )

Que 35. What is the purpose of the strip_tags() function in PHP?

Answer: The strip_tags() function removes HTML and PHP tags from a string, optionally allowing specific tags.

Example:

$string = "<p>Hello</p><b>World</b>";
echo strip_tags($string);  // Outputs: HelloWorld

Que 36. How do you use the array_reverse() function in PHP?

Answer: The array_reverse() function returns a new array with elements in reverse order.

Example:

$array = [1, 2, 3];
$reversed = array_reverse($array);
print_r($reversed);  // Outputs: Array ( [0] => 3 [1] => 2 [2] => 1 )

Que 37. What is the purpose of the is_array() function in PHP?

Answer: The is_array() function checks if a variable is an array, returning true or false.

Example:

$var = [1, 2, 3];
echo is_array($var);  // Outputs: 1 (true)

Que 38. How do you use the explode() function with a limit parameter in PHP?

Answer: The explode() function splits a string into an array using a delimiter, and the optional limit parameter controls the number of resulting elements.

Example:

$string = "a,b,c,d";
$array = explode(",", $string, 2);
print_r($array);  // Outputs: Array ( [0] => a [1] => b,c,d )

Que 39. What is the purpose of the error_reporting() function in PHP?

Answer: The error_reporting() function sets which PHP errors are reported, controlling error visibility.

Example:

error_reporting(E_ALL);  // Show all errors

Que 40. How do you use the array_search() function in PHP?

Answer: The array_search() function searches an array for a value and returns its key (or false if not found).

Example:

$array = ["a" => "apple", "b" => "banana"];
echo array_search("banana", $array);  // Outputs: b

Que 41. What is the difference between require and require_once in PHP?

Answer: require includes a file and stops with a fatal error if missing, while require_once ensures the file is included only once.

Example:

require 'config.php';      // May include multiple times
require_once 'config.php'; // Includes only once

Que 42. How do you use the urlencode() function in PHP?

Answer: The urlencode() function encodes a string to be safely used in a URL query string by converting special characters.

Example:

$string = "Hello World!";
echo urlencode($string);  // Outputs: Hello+World%21

Que 43. What is the purpose of the is_numeric() function in PHP?

Answer: The is_numeric() function checks if a variable is a number or a numeric string, returning true or false.

Example:

$var = "123";
echo is_numeric($var);  // Outputs: 1 (true)

Que 44. How do you use the array_diff() function in PHP?

Answer: The array_diff() function returns values in the first array that are not present in other arrays.

Example:

$arr1 = [1, 2, 3];
$arr2 = [2, 4];
print_r(array_diff($arr1, $arr2));  // Outputs: Array ( [0] => 1 [2] => 3 )

Que 45. What is the purpose of the parse_url() function in PHP?

Answer: The parse_url() function parses a URL and returns its components as an array.

Example:

$url = "https://example.com/path?query=1";
print_r(parse_url($url));  // Outputs: Array with scheme, host, path, query

Que 46. How do you use the str_pad() function in PHP?

Answer: The str_pad() function pads a string to a specified length with a given character.

Example:

$string = "Hello";
echo str_pad($string, 10, "*");  // Outputs: Hello*****

Que 47. What is the purpose of the gettype() function in PHP?

Answer: The gettype() function returns the type of a variable as a string.

Example:

$var = 123;
echo gettype($var);  // Outputs: integer

Que 48. How do you use the array_splice() function in PHP?

Answer: The array_splice() function removes a portion of an array and optionally replaces it with new elements.

Example:

$array = [1, 2, 3, 4];
array_splice($array, 1, 2, [5, 6]);
print_r($array);  // Outputs: Array ( [0] => 1 [1] => 5 [2] => 6 [3] => 4 )

Que 49. What is the purpose of the nl2br() function in PHP?

Answer: The nl2br() function converts newlines (\n) in a string to HTML <br> tags.

Example:

$string = "Line1\nLine2";
echo nl2br($string);  // Outputs: Line1<br>Line2

Que 50. How do you use the array_walk() function in PHP?

Answer: The array_walk() function applies a callback function to each element of an array.

Example:

$array = [1, 2, 3];
array_walk($array, fn(&$value) => $value *= 2);
print_r($array);  // Outputs: Array ( [0] => 2 [1] => 4 [2] => 6 )
OOPs Interview QuestionsTop Coding Interview Questions
Full Stack Developer Interview QuestionsSQL Interview Questions for Freshers
Web Development Interview QuestionsPHP Interview Questions for Experienced

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *