All PHP Interview Questions & Answers

All PHP Interview Questions & Answers list

Here, you can find PHP Interview Questions & Answers related to Arrays, Operators, Loops, Variables, Conditional Statements, Strings, Form, MySQL, Cookies, and Sessions. These popular & frequently asked interview questions are listed under different categories.

Tutorials Class covers Interview Questions for Freshers as well as Experienced professionals. We not only list core PHP but also provide Advanced PHP interview questions for students.

How many types of arrays are available in PHP?

There are three types of array available in PHP.

  1. Numeric (Indexed) array
  2. Associative array
  3. Multidimensional array

Learn more about array with examples in PHP Array Tutorial

What is the use of ucwords() PHP String function?

PHP ucwords() function is used to capitalized the first letter of each word in String.

<?php
echo ucwords("welcome to the php tutorials");
?>

Run : http://ideone.com/83vvyF

What is the use of array_chunk() function in PHP?

The array_chunk() function is used to split an array into parts or chunks of new arrays. Example: array_chunk(array,size);

The first parameter specifies an array and the second parameter defines the size of each chunk.

What is the use of str_replace() PHP String function?

The PHP str_replace() function is used to search & replace some words in a strings.

Example

<?php
echo str_replace("Hello", "Hi", "Hello PHP!");
?>

In this example, "Hello" word will be replaced by "Hi" in the given string.

Run : http://ideone.com/AFs0DW

What is the use of in_array() function in PHP?

The in_array() function is used to search for the given string in an array. It returns TRUE if the given string is found in the array, and FALSE otherwise.

Which PHP function counts all the values of an array?

array_count_values() function is used to count all the values of an array. PHP array_count_values() returns an array that has the values of the given array as keys and their frequency in the array as values.

What is the latest version of PHP?

The latest stable version of PHP is 7.3. As per the latest update, PHP 7.3.4 released on April 4, 2019.

For more detail about PHP version, visit official PHP website: php.net

How can we submit a form without a submit button?

Java script submit() function is used for submit form without submit button. For example: document.formname.submit()

<form action="page2.php" name="contactform">// Form elements</form>

Can we include a file two times in any PHP page?

Yes, we can use include("sample.php")

function more than once in any PHP page. It will include code from 'sample.php' file multiple times.

Note: Note: If ‘sample.php’ file contains some functions declaration, there will be an error. Why? Because it will try to separate function with the same name again and it will throw an error.

Difference Between Cookies and Session

PHP sessions improve upon cookies because they allow web applications to store and retrieve more information than cookies. PHP sessions actually use cookies, but they add more functionality and security.

Sessions store data on the server, not on the browser like cookies

The main difference between a session and a cookie is that session data is stored on the server, whereas cookies store data in the visitor’s browser. Sessions use a session identifier to locate a particular user’s session data. This session identifier is normally stored in the user’s web browser in a cookie, but the sensitive data that needs to be more secure — like the user’s ID, name, etc. — will always stay on the server.

Sessions are more secure than cookies

So, why exactly should we use sessions when cookies work just fine? Well, as we already mentioned, sessions are more secure because the relevant information is stored on the server and not sent back and forth between the client and server. The second reason is that some users either turn off cookies or reject them.

Difference Between Cookies and Session

Sr. No.CookiesSession
1.Cookies are client-side files that can store some data on browsersSessions are server-side files that can store some data on servers.
2.We have to set cookie life (or expiration time) using PHP function setcookie()
setcookie("mobile", "123", time() + (86400 * 30), "/"); 
In this example, Cookie Name is “mobile”, Cookie Value is “123” and expiration time is: I hour after the current time (1 Hour = 86400 Seconds)
Mostly, Session Max lifetime is 1440 Seconds (24 Minutes) as defined in php.ini file. You can change it.
3.In PHP, $_COOKIE superglobal variable is used to get cookie data.In PHP, $_SESSION superglobal variable is used to set or delete the session data.
4.You don’t need any function to start Cookie as It is stored in your local machine.Before using $_SESSION, you have to call session_start();
This function will start the session and then, you can access $_SESSION data on that page.
5.Most browser’s maximum Cookie size is 4KB (4096 Bytes)You can store as much data as you like within in sessions.The only limits you can reach is the maximum memory a script can consume at one time, which by default is 128MB.
6.You can delete a cookie by providing any past date in expiration time parameter, It will automatically delete the cookie. There is no separate function to unset cookie.PHP functionsession_destroy() is used to destroy all data stored to a session, and if you want to unset a particular session variable then you can use PHP unset() function. Example: unset($_SESSION["name"])
7.Cookie ends depends on the lifetime you set for it.Session ends when user close his browser.

What is the full form of PHP?

PHP originally stood for Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor.

How to find String Length in PHP?

The PHP strlen() function returns the length of a string. String length count will include space & special characters as well.
Syntax: strlen(string);

Example:

echo strlen("Hello world!");
Tutorials Class - Output Window

12

Which is the PHP function to delete a file from server?

PHP has lots of inbuilt Functions. One of them is the unlink() PHP function which is used to delete a file from server. Example: unlink("sample-file.php")

How can we create a session in php?

PHP session is a way to store information to be used across multiple pages. A session is started with the session_start() function. Session data is stored in the PHP global variable: $_SESSION.


Example to create a PHP SESSION

<?php
// Start the session
session_start();
// Set session variables
$_SESSION["name"] = "robin";
$_SESSION["fav_color"] = "green";
echo "Session variables are set: ".$_SESSION["name"]." ".$_SESSION["fav_color"];
?>

What is PHP floor() function?

With the help of the floor() function, we can convert a numeric value to it’s next lower integer value.


Example of floor() function in PHP:

<?php
$a=-145.13;
$result=floor($a);
echo "Floor value of $a is $result";    // Output: Floor value of -145.13 is -145
?>

How can we remove the white spaces from string in PHP?

There are three functions in PHP to remove the white spaces from the string.
trim()-It removes whitespaces from the left and right side of the string.
ltrim()-It removes whitespaces from the left side of the string.
rtrim()-It removes whitespaces from the right side of the string.

Example:

<?php
$string="  Interview  Question   and Answers for your help   ";
$var1=trim($string);
echo $var1;
$var2=ltrim($string);
echo $var2;
$var3=rtrim($string);
echo $var3;
?>

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

Both PHP functions are used to fetch data from MYSQL database, but there is a difference between mysqli_fetch_array() and mysqli_fetch_object() output format.

With the help of mysqli_fetch_array() function, we can fetch a query’s result data row as an array.

Example of mysqli_fetch_array() Function:

<?php
$conn=mysqli_connect("localhost","username","password","db");
$query="SELECT students, marks FROM class";
$result=mysqli_query($conn,$query);
while ($arr=mysqli_fetch_array($result))
{
    echo $arr['students']." ".$arr['marks'];
    echo "<br />";
}
mysqli_close($conn);
?>

While mysqli_fetch_object() function, we can fetch a query’s result data row as an object.

Example of mysqli_fetch_object() Function:

<?php
$conn=mysqli_connect("localhost","username","password","db");
$query="SELECT students, marks FROM class";
$result=mysqli_query($conn,$query);
while ($obj=mysqli_fetch_object($result))
{
    echo $obj->students." ".$obj->marks;
    echo "<br />";
}
mysqli_close($conn);
?>

How to check if a variable is empty in PHP?

The empty() function is an in-built function in PHP which is used to check whether a variable is empty or not.

Example of empty function:

<?php
$var1 = "";
if(empty($var1))
{
echo"Yes, variable is empty";
}
else
{
echo"No, variable is not empty";
}
?>

Here is the list of values, that are returned as an empty

  • “” (an empty string)
  • 0(as an integer)
  • 0.0(o as a float)
  • “0”(o as a string)
  • Null
  • False
  • array() (an empty array)

How to count the number of parameters passed in a PHP function?

With the help of func_num_args() function, we can count the number of parameters passed into a PHP function.

Example of function func_num_args()

<?php
function count_param()
{
    $count_args = func_num_args();
    echo "Number of parameters: $count_args\n";
}
 
count_param(10, 20);  // Output: Number of parameters: 2
 
?>

How can you pass a variable by reference in PHP?

By using an ampersand (&) sign in-front of the variable, we can pass the variable by reference.

Why or when to use variable by reference ?

When PHP function arguments are passed by value, and value is changed within function, it does not change variable value outside the function. To allow a function to modify its argument value outside the function, they must be passed by reference.

Example:

<?php
function add_line(&$line)
{
    $line.= 'and extra words added.';
}
 
$sample_line= 'This is a sample line, ';
add_line($sample_line);
echo $sample_line;    // outputs 'This is a sample line,  and extra words added.'
?>

If we do not use value by reference (&), then the output will be: ‘This is a sample line,’