Top PHP Interview Questions And Answers

1. What is PHP?

PHP is a web language based on scripts that allow developers to dynamically create generated web pages.

2. Is PHP a case sensitive language?

Is PHP a case sensitive language?

3. Why do we use PHP?

There are several benefits of using PHP. First of all, it is totally free to use. So anyone can use PHP without any cost and host the site at a minimal cost.

4. What is the correct and the most two common way to start and finish a PHP block of code?

The two most common ways to start and finish a PHP script are:

<?php /* Write PHP code */ ?> and <? /* Write PHP code */ ?>

5. What is escaping to PHP?

The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The mechanism for doing so is known as ‘escaping to PHP.’

6. What are the differences between echo and print?

Both echo and print method print the output in the browser but there is a difference between these two methods.

7. What type of operation is needed when passing values through a form or an URL?

If we would like to pass values through a form or an URL, then we need to encode and to decode them using htmlspecialchars() and urlencode().

8. Explain the syntax for ‘foreach’ loop.

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

foreach (array as value)
    code to be executed;

9. How failures in execution are handled with include() and require() functions?

If the function require() cannot access the file then it ends with a fatal error. However, the include() function gives a warning, and the PHP script continues to execute.

10. What is the main difference between require() and require_once()?

require(), and require_once() perform the same task except that the second function checks if the PHP script is already included or not before executing it.

11. What is numeric array?

Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.

12. What is associate array?

Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.

13. What is Multidimensional array?

Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.

14. What are the uses of explode() and implode() functions?

explode() function is used to split a string into an array and implode() function is used to make a string by combining the array elements.

$text = "I love programming";
print_r (explode(" ",$text));
$strarr = array('Pen','Pencil','Eraser');
echo implode(" ",$strarr);

15. How can we connect to a MySQL database from a PHP script?

To be able to connect to a MySQL database, we must use mysqli_connect() function as follows:

$database = mysqli_connect("HOST", "USER_NAME", "PASSWORD"); mysqli_select_db($database,"DATABASE_NAME"); 

16. How be the result set of Mysql handled in PHP?

The result set can be handled using mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_object or mysqli_fetch_row.

17. How is it possible to know the number of rows returned in the result set?

The function mysqli_num_rows() returns the number of rows in a result set.

18. Which function gives us the number of affected entries by a query?

mysqli_affected_rows() return the number of entries affected by an SQL query.

19. What is the difference between mysqli_fetch_object() and mysqli_fetch_array()?

The mysqli_fetch_object() function collects the first single matching record where mysqli_fetch_array() collects all matching records from the table in an array.

20. How will you redirect a page using PHP?

The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading. The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of rest of the code.

21. How will you get information sent via get method in PHP?

The PHP provides $_GET associative array to access all the sent information using GET method.

22. How will you get information sent via post method in PHP?

The PHP provides $_POST associative array to access all the sent information using POST method.

23. What is the purpse $_REQUEST variable?

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

24. How can you create a session in PHP?

session_start() function is used in PHP to create a session.

session_start(); //Start session
$_SESSION['USERNAME']='Fahmida'; //Set a session value
unset($_SESSION['USERNAME']; //delete session value

25. Which function you can use in PHP to open a file for reading or writing or for both?

You can use fopen() function to read or write or for doing both in PHP.

$file1 = fopen("myfile_1.txt","r"); //for reading
$file2 = fopen("myfile_2.txt","w"); //for writing
$file3 = fopen("myfile_3.txt","r+"); //for reading and writing

26. Which function is used in PHP to delete a file?

unlink() function is used in PHP to delete any file.


27. How is a constant defined in a PHP script?

The define() directive lets us defining a constant as follows:

define ("ACONSTANT", 143);

28. How will you set cookies using PHP?

PHP provided setcookie() function to set a cookie. This function requires upto six arguments and should be called before <html> tag. For each cookie this function has to be called separately.

setcookie(name, value, expire, path, domain, security);

29. How will you get cookies using PHP?

PHP provides many ways to access cookies. Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables.

30. What is the difference between substr() and strstr()?

substr() function returns a part of the string based on the starting point and length. Length parameter is optional for this function and if it is omitted then the remaining part of the string from the starting point will be returned.

strstr() function searches the first occurrence of a string inside another string. The third parameter of this function is optional and it is used to retrieve the part of the string that appears before the first occurrence of the searching string.

echo substr("Computer Programming",9,7); //Returns “Program”
echo substr("Computer Programming",9); //Returns “Programming”
echo strstr("Learning Laravel 5!","Laravel"); //Returns Laravel 5!
echo strstr("Learning Laravel 5!","Laravel",true); //Returns Learning

31.  Which function is used in PHP to search a particular value in an array?

in_array() function is used to search a particular value in an array.

$languages = array("C", "C++", "Java", "PHP");
if (in_array("PHP", $languages)) {
echo "PHP is in the list";
else {
echo "php is not in the list";

32. What does $_SERVER mean?

$_SERVER is an array including information created by the web server such as paths, headers, and script locations.

33. What does $_FILES means?

$_FILES is an associative array composed of items sent to the current script via the HTTP POST method.

34. What is the difference between $_FILES[‘userfile’][‘name’] and $_FILES[‘userfile’][‘tmp_name’]?

$_FILES[‘userfile’][‘name’] represents the original name of the file on the client machine,

$_FILES[‘userfile’][‘tmp_name’] represents the temporary filename of the file stored on the server.

35. How can we change the maximum size of the files to be uploaded?

We can change the maximum size of files to be uploaded by changing upload_max_filesize in php.ini.

36. How will you get the current date and time using PHP?

PHP’s time() function gives you all the information that you need about the current date and time. It requires no arguments but returns an integer.

echo "Today is " . date("Y/m/d") . "<br>";

echo "The time is " . date("h:i:sa");

37. What is the purpose of date() function?

The date() function returns a formatted string representing a date. You can exercise an enormous amount of control over the format that date() returns with a string argument that you must pass to it.

38. What type of errors can be occurred in PHP?

Some major error types are mentioned below:

Fatal Errors– The execution of the script stops when this error occurs.

Parse Errors– This type of error occurs when the coder uses a wrong syntax in the script.

Warning Errors- This type of error does not stop the execution of a script. It continues the script even after displaying the error.

Notice Errors- This type of error shows a minor error of the script and continues the execution after displaying the error.

39. How can we determine whether a variable is set?.

The boolean function isset determines if a variable is set and is not NULL.

40. Is it possible to protect special characters in a query string?

Yes, we use the urlencode() function to be able to protect special characters.

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply

Your email address will not be published.