50+ PHP Interview Questions And Answers For Freshers


Hello Friends, after a long time i am here with a great list of interview questions and answers. From last couple of months, I was looking for changing my job. Therefore, I had updated my resume on naukari and start giving interviews. Based on that experience i create some list of PHP interview questions and answers. So read it and enjoy it.

Q. What is PHP? Full form of PHP?
PHP is most popular server side scripting language. PHP stands for PHP: Hypertext Preprocessor

Q. How many types of error in PHP? Explain little about every error.
There are three main type error in PHP.

  • Notices: As the name says, Notices are errors which occur while you may not initialize variable or any other things. This will generate only notice on the view page. This will not terminate the running script.
  • Warnings: This is run-time warnings and does not terminate the script execution. This error mostly occurs when you forgot to include () a file.
  • Fatal Errors:
    These are most critical error of the PHP. It can terminate your script. Example of this error is forgotten to semicolon (;) in the script.

Q. How to initialize error in PHP?
1) error_reporting(E_ALL);
2) ini_set(‘display_errors’, 1);

Q. What is difference between EMPTY ($variable = “”) or NULL ($variable = NULL) variable in PHP?
Null means nothing. It’s just a literal. Null is the value of reference variable. But empty string is blank. It gives the length=0. Empty string is a blank value, means the string does not have anything. (Source from Stack overflow)

Q. How to define array in PHP?

  • Method – 1
    $firstdata = array(“value-1”, “value-2”, “value-3”, “value-4”);
  • Method – 2
    $seconddata[] = “value-5”;
    $seconddata[] = “value-6”;
    $seconddata[] = “value-7”;

Q. How to find substring from string?
You can use strpos() function to find substring in string. Check out this example.

Q. What is Session and Cookie?

  • Session: A session is a way to store data on the web-server. You can use session variables throughout all pages of your application. Normally session will destroy when you close the browser.
  • Cookie: A Cookie is a way to store data on the user’s computer. It is mostly used for identify user. You can set cookie, get cookie or delete cookie by php functions.

Q. How to redirect page in PHP?
header(‘Location: http://www.example.com/’);

Related:  [SOLVED] PHP is not recognized as an internal or external command

Q. Which are most common methods for sending form data?
GET, POST (Most Famous Two Methods)

Q. What is difference between GET and POST Method?
GET and POST are two most popular HTTP methods normally uses for send data from one page to another page.

  • GET :GET is usually intended to retrieve some data. Information sent using GET method is visible to user. You cannot sent sensitive information using GET method.
  • POST :POST is mostly used to send form data from one page to another. You can send sensitive information using POST method. POST can pass much more information to the server and can be of almost any length.

Q. What is use of isset() function in PHP?
isset() function is used to check variable is initialize or not. Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

Q. How to get current date and time using PHP?
date(‘d-m-Y h:i:s’);

Q. How to retrieve session data in PHP?
You can use $_SESSION to retrieve session variables.

Q. How to destroy session?
You can use destroy($_SESSION); or unset($_SESSION['your-variable']);

Q. How to set, get and delete cookie in php?
For setting up cookie : setcookie("TestCookieName", "TestCookieValue", "Timespan");
For Get cookie : $_COOKIE["TestCookieName"];
For Delete Cookie : unset($_COOKIE['TestCookieName']);Setcookie("TestCookieName", null, time()-3600);

Q. How to send form data on same page using php?
Here is a code for form

<form action="" method="POST">
<input type="text" name="firstname">
<input type="submit">

Q. What is $_REQUEST in php? Where to use it?
An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.

Q. How to get url in PHP?

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $actual_link

Q. How to get system name in PHP?


Q. How to send mail using PHP?


Q. How to concate two string in PHP?
You can concate two string by using (.) Dot Operator.

echo $string1. $string2 ;

Q. How to use ternary operator in PHP?
The syntax of ternary operator is as below
Syntax: Condition? true: false

($king == 'raja') ? 'raja' : 'rani' ;

Q. What is different between “echo()” and “print()” in php?
Both are used to output a data. Echo is slightly faster than print. Echo does not return any value while print return 1.

Q. What difference is between include and require?
Based on this answer on Quora,

  • include() will attempt to load the specified file, but will allow the script to continue if not successfully loaded.
  • require(), on the other hand will cause a “Fatal Error” to occur if the specified file is not successfully loaded.
Related:  30+ core PHP examples and tutorials for beginner

Use require() if you want the safeguard the script from completing upon failure to load external file.

Q. What is difference between include and include_once()? (Answer of next question is also here).
The only difference between the two is that require and its sister require_once throw a fatal error if the file is not found, whereas include and include_once only show a warning and continue to load the rest of the page. If you don’t want PHP to attempt to load the rest of your page without the database info (which I would assume), then use require_once. You don’t need to include the file more than once, so there is no need to use the regular require function. (courtesy : stack overflow )

Q. What is difference between require and require_once()?
Check out the answer of above question.

Q. Which array function is used for count array elements?

Q. How many loops in php?
Loops exists in PHP

  • For loop
  • Foreach loop
  • While loop
  • Do…While loop

Q. How to connect with MySQL database using PHP?

$conn = mysqli_connect('DB hostname','DB username','DB password','DB name');

Q. What is use of explode and implode functions?
Use of explode() and implode() function

  • explode(): Convert string to Array
  • implode(): Convert Array to string

Q. How to get length of string in PHP?
To get a length of string use strlen() function

Ex. echo strlen($string)

Q. Write html form code form with upload file?
Here is a html code for file upload

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="upload_file">
<input type="submit" name="add_file">

Q. How to delete file in php?


Q. How to get first and last element of array in php?
For first element: current($arrayname);
For last element: end($arrayname);

Q. How to print array in php?
Use print_r($array); function to print array.

Q. How to get random number in php?
Use rand() function to generate random number

Q. How to sort array in PHP?
Here are array sorting functions in php

  • sort() – sort arrays in ascending order
  • rsort() – sort arrays in descending order
  • asort() – sort associative arrays in ascending order, according to the value
  • ksort() – sort associative arrays in ascending order, according to the key
  • arsort() – sort associative arrays in descending order, according to the value
  • krsort() – sort associative arrays in descending order, according to the key
Related:  Load More Content While Scrolling With jQuery Ajax and PHP

Q. Methods to encrypt password in PHP
password_hash() – Uses a strong hash, generates a strong salt, and applies proper rounds automatically.
md5() – Note that md5() is not secure now days. Please take care.
bcrypt() – The salt parameter is optional. However, crypt() creates a weak password without the salt.

Note : Note that md5() is not secure now days. Please take care.

Q. What is difference $message and $$message?
if $message = “one”; then
$$message = $one (Exp. for “one” is behave as a variable for $$message);

Q. What is difference between “==” and “===” operator in php?
"==" checks the left and right values are equal or not whether "===" check the left and right values are equal with their equal types (Eg. Ints, Booleans)

Q. Is PHP’s functions are case-sensitive?
No, They are not. But as the manual suggest “Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.”

Q. Show me example of GOTO statement.

goto a;
echo 'Foo';
echo 'Bar';

Q. How to delete element from array in php?
Use unset(array_element) function to delete array.

Q. Which function is used to dump the variable?

Q. Which function is used to remove blank element from array?

Q. What is purpose to use array_chunk() function?
array_chunk(); function is used to split array element in group of small arrays.

Q. How to get a sum of array element?
array_sum() function is used to get total sum of array elements.

Q. How to get unique value from array?
You can use array_unique() function to get unique values.

So these are the PHP interview questions and answers for beginner. Please let me know your view about this questions and answers. Do share your questions and thoughts as well with us in the comment box. Please do share so people can enjoy it. Thank you very much. Happy Coding 🙂

About Chintan Panchal

I am web developer having 6+ year of experience of web development. Currently working on laravel and have worked on codeigniter, cakephp, symfony. Thank you :)

View all posts by Chintan Panchal →

One Comment on “50+ PHP Interview Questions And Answers For Freshers”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.