PHP Interview Questions And Answers

Basic to Advanced question

Mosharrf Hossain
Mh Mohon
2 min readMar 22, 2019

--

Q) How to include a file to a php page?

We can include a file using “include() ” or “require()” function with file path as its parameter.

eg:

<?php include('file_name'); ?>

Q) What’s the difference between include and require?

If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

Q) Differences between GET and POST methods ?

Both GET and POST method is used to transfer data from client to server in HTTP protocol but main difference between POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from client to server in http protocol.

Q) What is the difference between explode() and split() functions?

Split function splits string into array by regular expression. Explode splits a string into array by string.

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

Q) What is the difference between Session and Cookie?

The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user’s computers in the text file format. Cookies can not hold multiple variables,But Session can hold multiple variables.We can set expiry for a cookie,The session only remains active as long as the browser is open.Users do not have access to the data you stored in Session,Since it is stored in the server.Session is mainly used for login/logout purpose while cookies using for user activity tracking.

Q) What are the different types of errors in PHP ?

Here are three basic types of runtime errors in PHP:
Notices: These are trivial, non-critical errors that PHP encounters while executing a script — for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all — although you can change this default behavior. (Syntax Error)
Warnings: These are more serious errors — for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
Fatal errors: These are critical errors — for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.

Q) What will the result?

$a = 10;echo $a;
echo '$a';
echo "$a";

Ans:

10
$a
10

Q) What will the result?

$a = 016;echo $a/2;

Ans:

7

PHP interprets an integer with a leading 0 as an octal (base 8) number. 016 base 8 is 14 base 10. Converting octal to decimal by 016 = (0 ×8²) + (1 × 8¹) + (6 × 8⁰) = 14

--

--