PHP Developer Interview Questions and Answers for Freshers Part 1

PHP Developer Interview Questions:

Table of Contents

1. What is PHP?

Answer: PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language designed for web development, but also used as a general-purpose programming language.

2. What is the difference between PHP and HTML?

Answer: HTML is a markup language used to create the structure of web pages, whereas PHP is a server-side scripting language used to create dynamic content that interacts with databases.

3. How do you start and end a PHP script?

Answer: A PHP script starts with <?php and ends with ?>.

4. What is a variable in PHP?

Answer: A variable in PHP is a symbol or name that holds a value. Variables in PHP start with a $ sign followed by the name of the variable.

5. How do you declare a variable in PHP?

Answer: A variable is declared using the $ sign, followed by the variable name. For example, $variable_name = value;.

6. What are the common data types in PHP?

Answer: The common data types in PHP include strings, integers, floats, booleans, arrays, objects, NULL, and resources.

7. What is an array in PHP?

Answer: An array in PHP is a special variable, which can hold more than one value at a time. Arrays can be indexed or associative.

8. How do you create an indexed array in PHP?

Answer: An indexed array can be created like this: $array = array(1, 2, 3, 4);.

9. How do you create an associative array in PHP?

Answer: An associative array can be created like this: $array = array("key1" => "value1", "key2" => "value2");.

10. How do you iterate through an array in PHP?

Answer: You can iterate through an array using a foreach loop:

foreach ($array as $value) {
echo $value;
}

or for associative arrays:

foreach ($array as $key => $value) {
echo "$key: $value";
}

11. What is the difference between echo and print in PHP?

Answer: Both echo and print are used to output data to the screen. echo can output multiple strings separated by commas and has no return value, while print can only output one string and always returns 1.

12. What are superglobals in PHP?

Answer: Superglobals are built-in variables that are always accessible, regardless of scope. Examples include $_GET, $_POST, $_SESSION, $_COOKIE, $_REQUEST, $_SERVER, and $_FILES.

13. How do you get data from a form using PHP?

Answer: You can get data from a form using the $_GET or $_POST superglobal arrays depending on the form method. For example:

$name = $_POST['name'];

if the form method is POST.

14. How do you include one PHP file within another PHP file?

Answer: You can include one PHP file within another using include() or require(). For example:

include 'file.php';

or

require 'file.php';

15. What is the difference between include and require?

Answer: The difference is that require() will produce a fatal error and stop the script if the file cannot be included, whereas include() will only produce a warning and the script will continue to execute.

16. How do you connect to a MySQL database in PHP?

Answer: You can connect to a MySQL database using the mysqli_connect() function. For example:

$connection = mysqli_connect("hostname", "username", "password", "database");

17. How do you execute a SQL query in PHP?

Answer: You can execute a SQL query using the mysqli_query() function. For example:

$result = mysqli_query($connection, "SELECT * FROM table_name");

18. How do you fetch data from a MySQL result set in PHP?

Answer: You can fetch data using mysqli_fetch_assoc(), mysqli_fetch_array(), mysqli_fetch_row(), or mysqli_fetch_object(). For example:

while ($row = mysqli_fetch_assoc($result)) {
echo $row['column_name'];
}

19. What is the purpose of mysqli_real_escape_string()?

Answer: The mysqli_real_escape_string() function is used to escape special characters in a string for use in an SQL query, preventing SQL injection attacks.

20. What is prepared statement in PHP and why is it useful?

Answer: Prepared statements are used to execute SQL queries securely by separating SQL logic from data. They prevent SQL injection by ensuring that data is correctly escaped. They are created using mysqli_prepare() and executed with mysqli_stmt_bind_param() and mysqli_stmt_execute().

21. How do you start a session in PHP?

Answer: You start a session using the session_start() function at the beginning of your script.

22. How do you store and retrieve data in a PHP session?

Answer:

// Store data
$_SESSION['username'] = 'JohnDoe';
// Retrieve data
$username = $_SESSION[‘username’];

23. How do you destroy a PHP session?

Answer:

session_start();
session_unset();
session_destroy();

24. What is a cookie in PHP?

Answer: A cookie is a small file stored on the user’s computer that holds data about the user or their activity.

25. How do you set a cookie in PHP?

Answer:

setcookie("username", "JohnDoe", time() + (86400 * 30), "/");

This sets a cookie named “username” with the value “JohnDoe” that expires in 30 days.

26. How do you retrieve a cookie value in PHP?

Answer:

$username = $_COOKIE['username'];

27. What is the use of the header() function in PHP?

Answer: The header() function is used to send raw HTTP headers to the browser. It is commonly used for redirects or content type changes.

28. How do you redirect a user to another page in PHP?

Answer:

header("Location: another_page.php");
exit();

29. What is isset() function in PHP?

Answer: The isset() function checks if a variable is set and is not NULL. It returns true if the variable exists and is not NULL.

30. What is empty() function in PHP?

Answer: The empty() function checks whether a variable is empty. It returns true if the variable does not exist or its value equals false.

31. How do you handle errors in PHP?

Answer: Errors in PHP can be handled using custom error handlers with set_error_handler(), using error reporting functions like error_reporting(), or using try-catch blocks for exceptions.

32. What are the different error levels in PHP?

Answer: Some common error levels include E_ERROR, E_WARNING, E_PARSE, E_NOTICE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and E_USER_* levels.

33. What is the difference between require_once and include_once?

Answer: Both require_once and include_once ensure the file is included only once, but require_once produces a fatal error if the file cannot be included, while include_once produces a warning.

34. How do you create a function in PHP?

Answer:

function functionName() {
// Code to be executed
}

35. How do you pass arguments to a PHP function?

Answer:

function functionName($arg1, $arg2) {
// Code to be executed
}

36. What is the purpose of the return statement in PHP?

Answer: The return statement is used to end the execution of a function and return a value to the calling function or code.

37. What are PHP constants and how do you define them?

Answer: Constants are name or identifier for a simple value. They are defined using the define() function and cannot be changed once set. For example:

define("SITE_NAME", "My Website");

38. How do you define an object in PHP?

Answer:

class MyClass {
// Properties and methods
}
$object = new MyClass();

39. What is inheritance in PHP?

Answer: Inheritance is an OOP concept where a class (child class) inherits properties and methods from another class (parent class). It is implemented using the extends keyword.

40. How do you access parent class methods in PHP?

Answer: You can access parent class methods using the parent:: keyword within the child class.

41. What is polymorphism in PHP?

Answer: Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. Methods in different classes can share the same name but can have different implementations.

42. How do you handle exceptions in PHP?

Answer: Exceptions in PHP are handled using try-catch blocks. For example:

try {
// Code that may throw an exception
} catch (Exception $e) {
// Code to handle the exception
}

43. What is the difference between == and === in PHP?

Answer: == checks for value equality, while === checks for value and type equality.

44. What is the use of explode() and implode() functions in PHP?

Answer: explode() splits a string into an array by a delimiter, while implode() joins array elements into a string with a delimiter.

45. How do you create a class in PHP?

Answer:

class MyClass {
// Properties and methods
}

46. What is a constructor in PHP?

Answer: A constructor is a special function in a class that is automatically called when an object of the class is instantiated. It is defined using __construct().

47. What is the use of __destruct() method in PHP?

Answer: The __destruct() method is called when an object is destroyed. It is used for cleanup tasks.

48. What is an interface in PHP?

Answer: An interface defines methods that must be implemented by a class. Interfaces are declared using the interface keyword and classes implement them using the implements keyword.

49. How do you declare an interface in PHP?

Answer:

interface MyInterface {
public function myMethod();
}

50. How do you implement an interface in PHP?

Answer:

class MyClass implements MyInterface {
public function myMethod() {
// Method implementation
}
}