Interview Questions




Most Essential And Frequently Asked Interview
Questions And Answer

PHP

Q.What are the difference between mysqli_fetch_assoc and mysqli_fetch_array?

Ans.:

PHP-MYSQL |  mysqli_fetch_assoc() VS  mysqli_fetch_array()

mysqli_fetch_assoc() function fetch a result row as an associative array where as mysqli_fetch_array() function fetch a result row as an associative array, a numeric array, or both.

View Detials

Q.How will you concatenate two strings in PHP?

Ans.:

PHP | Combine two strings

There are several ways to concatenate two strings in PHP.

Use the concatenation operator [.] and [.=]

There are two string operators that are used for concatenation.

[.]:  Concatenation operator | Used to combine two string values to create a new string.
[.=]: Concatenation assignment operator | Appends the argument on the right side to the argument on the left side.

View Detials

Q.how many ways we can retrieve the data in the result set of MySQL using PHP?

Ans.:

PHP-MYSQL |  Fetch Recoards From MySql table

There are 4 ways:

  1. mysqli_fetch_row() - Get a result row as an enumerated array
  2. mysqli_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
  3. mysqli_fetch_object() - Fetch a result row as an object
  4. mysqli_fetch_assoc() - Fetch a result row as an associative array
View Detials

Q.Is PHP a case sensitive language?

Ans.:

PHP | Case Sensitive

In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive but all variable names are case-sensitive.

View Detials

Q.What are the difference between mysqli_fetch_object and mysqli_fetch_array?

Ans.:

PHP-MYSQL |  mysqli_fetch_object () VS  mysqli_fetch_array()

mysqli_fetch_object()  fetch a result row as an object where as mysqli_fetch_array()  fetch a result row as an associative array, a numeric array, or both.

View Detials

Q.What is the difference between $name and $$name?

Ans.:

$name and $$name both are variables but with a single difference that is $name is just a normal php variable where as $$name is known as reference variable. It allow you to use $name variable's value as a variable for getting $$name variable value. 

View Detials

Q.What is the difference between explode() and implode() function?

Ans.:

 PHP | implode() and explode() function

implode() function is use to convert an array into a string where as explode() function is used to convert an string into an array

View Detials

Q.How can we get the current session id in PHP?

Ans.:

session_id() function

There is a function session_id() in php which is use to get and/or set the current session id.

string session_id ([ string $id ] )

for getting session id in php we have to use session_id() function without parameter.

echo session_id()

if we want to set session id then we need to pass a parameter inside session_id() function. For this purpose we needs to be called session_id() function before session_start()

Q.Write a example code showing the nested ternary conditional operator in PHP?

Ans.:

PHP | Nested ternary conditional operator

An example code showing the nested ternary conditional operator in PHP.

<?php

$background_color = ($num == 0 ? 'blue' : ($num > 0 ? 'green' : 'red'));

?>

Here we are assigning different strings to the $background_color variable based on a numeric value ($num). 

Q.How to stop execution of PHP script ?

Ans.:

PHP | Stop the execution of php script | exit() funtion

By using exit() function we can stop the execution of php script.

View Detials

Q.What is session in php?

Ans.:

Session in php

A session is a global variable which is used to store users/sensitive information on the server. Each session register a unique session id which is use to fetch stored value. Session store data as an associative array. Once a session variable is created then it is used across multiple page. By default, session variables last until the user closes the browser or leave the site otherwise most of the server terminate the session variable if you are not active more than 24 minutes on the site.

View Detials

Q.What are the difference between Notify url and Return url?

Ans.:

PayPal | Notify url and Return url

Both of these are used in PayPal payment gateway integration.

Notify url is used by paypal to send response regarding current transaction in other word notify about the current transaction done in your site.

<input type="hidden" name="notify_url" value="https://www.yoursite.com/your_ipn.php">

By paypal: “Instant Payment Notification (IPN) is a message service that automatically notifies merchants of events related to PayPal transactions.”

Return url is use to specify the returning url after current transaction done. It is a redirect page url which is specified by the developer. Something link a page having "Thank you for your payment" message.

<input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php">

Q.What is difference between strstr() and stristr() function ?

Ans.:

strstr() and stristr()

strstr() and stristr() both are use to find first occurence of a string, but the differences are strstr( ) is case sensitive where as stristr( ) is case insensitive.

View Detials

Q.What is php.ini and .htaccess file?

Ans.:

php.ini and .htaccess file

Both php.ini and .htaccess file is the configuration file.

php.ini:

It is a configuration file for php setting. It is a special file by which you can make changes in PHP settings. php.ini file provide many setting such as changing of the default max_execution_time = 30, error_reporting = E_ALL & ~E_NOTICE, file_uploads = [on/off]  etc.

 

.htaccess:

It is the configuration file for server settings. With the help of the .htaccess file, you can make changes in server settings. It is supported by several web servers, including the popular Apache software which is used by most commercial web hosting providers. These files were first used to control user access on a per-directory basis. It is used to control a couple of things such as error handling, enabling SSL, IP blocking and IP whitelisting, URL redirects and URL rewriting, etc.

Q.How will you check if a file exists or not using PHP?

Ans.:

PHP | file_exists( ) Function

The file's existence can be confirmed using the file_exist() function which takes the file name as an argument.

file_exist("abc.jpg")
View Detials

Q.What is the use of header() function in php?

Ans.:

header() function

  1. The header() function in php is use to sends a raw HTTP header to a client.
  2. With the help of header() function we can Change page location, set timezone, set caching control, etc.
  3. It does not return any value means it is a void type
  4. It accept three parameter(i.e.: string, replace and http_response_code)
  5. It is important to Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
View Detials

Q.How we can remove html tags from string?

Ans.:

PHP | Remove html tags from a string

The strip_tags() function is use to remove any HTML, XML, and PHP tags from a string.

View Detials

Q.What is recursion in PHP?

Ans.:

PHP | Recursion 

PHP also supports recursive function call like C/C++. In such a case, a function calls itself within the function.

View Detials

Q.How can we remove duplicate value from an array in php?

Ans.:

PHP | Remove duplicate value from an array 

Duplicate value can remove from an array by using the array_unique function. If two or more array values are the same, the first appearance will be kept and the other will be removed. It takes an input array and returns a new array without duplicate values.

array_unique(array)
View Detials

Q.What are the predefined classes in PHP?

Ans.:

Predefined classes in PHP are:

  1. Directory
  2. stdClass
  3. __PHP_Incomplete_Class
  4. exception
  5. php_user_filter

Q.What are the main error type in php?

Ans.:

Error type in php

There are three main error type in php

  1. Notice Error
  2. Warning Error
  3. Fatal Error
View Detials

Q.How can you tell if a number is even or odd without using any condition or loop?

Ans.:

PHP | check whether a number is even or odd

A trick to check whether a number is even or odd without using any condition or loop.

View Detials

Q.What is php?

Ans.:

PHP | What is php?

PHP is a popular server site scripting langage stands for "PHP: Hypertext Preprocessor".  It is an open source scripting language, hence it is free to download and use. We can create dynamic and interactive web application, website or web pages wtih the help of PHP. Most of popular website bilt on php, such as Facebook, Google, Yahoo etc.

PHP file contain html, css, javascript and php code. Most used php file extension is ".php".


Why PHP?

  • It is open source, hence it is free to download and use.
  • Easy to learn.
  • It support a wide range of database. (e.g: mysql, Oracal, odbc etc.)
  • Compatatible with most of the server used todays. (e.g: Apache, IIS etc.)
  • It's run on various platforms. (e.g: Windows, Linux, Max OS X etc.)

PHP Syntex

PHP code start with "<?php" php opening tag and end wtih "?>" php closing tag. PHP code must be inside php opening and closing tags.

<?php
Your code here...
?>

<html>
<body>
<?php
echo "Hello PHP";
?>
</body>
</html>

Q.Why we use isset in PHP?

Ans.:

PHP | isset()

The isset () function is used to check whether a variable is set or not. It returns true if a variable is set otherwise it returns false.

View Detials

Q.What is the difference between unlink and unset function?

Ans.:

unlink() and unset()

The unlink() and unset() function in php have same functionality (i.e.: delete) but the difference is in the type of data on which they are applied to perform such delete functionalities.

unlink() used to delete files from directory where as unset() is used to unset or delete the variable.

View Detials

Q.How to get the client IP address in PHP?

Ans.:

$_SERVER['REMOTE_ADDR'] id use to get the IP address of the user.

View Detials

Q.What is the difference between a session and cookies in PHP?

Ans.:

Session V/S Cookies

A cookie is an array of data stored by the browser on the client location and sent to the server with every request. It stores a limited amount of data about 4kb(i.e.: 4096 bytes). It is less secure than a session because it present on the client's computer.

The session is an array of data stored on the server and associated with a given user (usually via a cookie containing an id code). It stores an unlimited amount of data. It is more secure because of stored on the server.

View Detials

Q.What will be the output of the following statements and why?
var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);

 

Ans.:

PHP | Output of the following statements var_dump(0123 == 123); var_dump('0123' == 123); var_dump('0123' === 123

 

var_dump(0123 == 123)

This will output bool(false) because the leading 0 in 0123 tells the PHP interpreter to treat the value as octal rather than decimal, so the value of 0123 in decimal is equal to 83. so it is clear that 83 is not equal to 123 and will return false.

var_dump('0123' == 123) 

This will output bool(true) since the string '0123' will automatically be converted to integer 123 and the leading 0 is ignored. Now both are the same so it will return true.

var_dump('0123' === 123) 

This will outputs bool(false) since "===" is an identical operator that checks value as well as data type, so it performs a more strict comparison and does not do the automatic type conversion of string to an integer. Hence will return false.

Q.What is the difference between explode and split?

Ans.:

The explode() and split() both function is use to split a string into an array. But the difference is explode() function splits a string into array by string where as Split function splits string into array by regular expression.

View Detials

Q.How do you define a constant in php?

Ans.:

Constant in PHP

The define() function is used to defining a constant in PHP

define("GREETING", "Welcome to StudyZone4U.com");
View Detials

Q.What is access specifiers in php?

Ans.:

PHP | Access specifiers

An access specifier is a code element that is used to determine which part of the program is allowed to access a particular variable or other information.

View Detials

Q.What is the maximum size of a file that can be uploaded using PHP and how can we change this?

Ans.:

PHP | The maximum size of a file that can be uploaded using PHP

The default maximum size of a file that can be uploaded using PHP is 2MB.

upload_max_filesize = 2M;

and we can change the maximum size by changing the value of upload_max_filesize in our php.ini file.

upload_max_filesize = 5M;

Q.How can we find the number of rows in a result set using PHP?

Ans.:

PHP | Find the number of rows in a result set

Bellow code is use for the same

$sql = "SELECT * FROM table1";
$result = mysql_query($sql, $db_link);
$num_rows = mysql_num_rows($result);
echo $num_rows;

 

Q.How can we increase the execution time of a PHP script?

Ans.:

PHP | max_execution_time directive in php.ini file

By changing the value of  max_execution_time in the php.ini file

max_execution_time = 30; // in second
View Detials

Q.How to avoid errors in PHP ?

Ans.:

PHP | How to avoid errors in PHP?

We can use PHP inbuilt function error_reporting(0) to avoid all errors in PHP file.

<?php

error_reporting(0);

?>

 

Q.what is list in php?

Ans.:

List 

List is a language construct and is similar to an array. It is used to assign a list of variables in one operation. If you are using PHP 5, then the list values start from a right parameter, and if you are using PHP 7, then the list starts with left parameter.

View Detials

Q.what is Cross-site scripting?

Ans.:

Cross-site scripting (XSS)

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side script into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy.

Q.How can we find or count number of elements in an array?

Ans.:

PHP | Count number of elements in an array

There are two ways to find the number of elements in an array:

1. sizeof($arr), This function is an alias of count()

echo sizeof($arr);

2. count($arr)

echo count($arr);

 

Q.What is use of htmlentities function in PHP?

Ans.:

PHP | htmlentities()

The htmlentities() function is used to convert all applicable characters to HTML entities.

View Detials

Q.How we can increase default timeout of session ?

Ans.:

PHP | Increase default session  timeout

The "session.gc_maxlifetime" is use to modify the default timout of session.

server should keep session data for AT LEAST 1 hour by using the below code. It increase session default timeout by 1 hour.

<?php

    //3600 is use for 1 hour

    ini_set('session.gc_maxlifetime', 3600);

?>

 

Q.What type of inheritance supported by php?

Ans.:

PHP | Inheritance

PHP mainly supports two types of inheritance:

  • Single inheritance
  • Multilevel inheritance

Q.What are default session time in PHP?

Ans.:

PHP | What are default session times in PHP?

By default, session variables last until the user closes the browser or leaves the site otherwise most of the server terminates the session variable if you are not active for more than1440 sec or 24 minutes on the site.

Q.What are cookies in PHP?

Ans.:

PHP | What are cookies in PHP?

A cookie is a small piece of information store by the browser on the client location and sent to the server with every request. It stores a limited amount of data about 4kb(i.e.: 4096 bytes).

View Detials

Q.How to upload file in PHP?

Ans.:

PHP | Upload File

By using move_uploaded_file() function we can upload file in PHP.The move_uploaded_file() function moves an uploaded file to a new location.

This function returns TRUE on success or FALSE on failure.

move_uploaded_file(file,newloc)
View Detials

Q.what are php magic fuction?

Ans.:

PHP | magic function

In PHP all functions start with __ names are magical functions. Magical functions always live in a PHP class. The definition of the magical function is defined by the programmer itself.

Here are list of magic functions available in PHP

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo().

Q.How to send mail in PHP ?

Ans.:

Send mail in PHP

The mail() function allows you to send emails directly from a script.

mail(to,subject,message,headers,parameters);
View Detials

Q.How to find the length of a string in PHP?

Ans.:

PHP | Length of a string

There is a string function called strlen() which is used to find the length of a string. It accepts a string that returns the length of the given string. 

strlen(string);
View Detials

Q.How we can find last insert id in table?

Ans.:

MySQL | Find last insert id in the table

By using the MySQL function mysqli_insert_id() we can retrieve the last inserted id in the database table.

View Detials

Q.How to redirect a page in PHP?

Ans.:

PHP | Redirect

PHP header() function is used to redirect a page to another page. It supplies raw HTTP headers to the browser and redirects 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 the rest of the code.

header(location: www.studyzone4u.com); // This will redirect to www.studyzone4u.com.

 

Q.What is difference between == and === operator ?

Ans.:

PHP | What is difference between == and === operator?

"==" and "==="

The two operators are known as comparison operators. "==" (i.e. equal)  and === (i.e. identical) both are use to check values are same or not. The difference between the two is that '==' should be used to check if the values of the two operands are equal or not whereas, '===' checks the values as well as the type of operands.

View Detials

Q.How can you enable errors in PHP ?

Ans.:

Enable errors in PHP

There are two ways to enable error reporting in your PHP scripts:

1. You can add the following function in the the script:

error_reporting(E_ALL);

2. You can add the following option in your website's php.ini file:

error_reporting = E_ALL

 

Q.How to find current date and time in PHP?

Ans.:

PHP |  date() function

By using the PHP date() function we can find the current date and time.

<?php

echo date(“d-m-Y”); //This will print today date.

echo date (“d-m-Y h:i:s”) // This will print today’s date with current time.

?>

 

Q.What are the errors in below code? What will be output and how can you fix it?
<?php

$arr = array();
$arr['var1'] = array(1, 2);
$arr['var2'] = 3;
$arr['var3'] = array(4, 5);

$test = array();

$test = array_merge($test, $arr['var1']);
var_dump($test);

$test = array_merge($test, $arr['var2']);
var_dump($test);

$test = array_merge($test, $arr['var3']);
var_dump($test);

?>

 

Ans.:

PHP | Array error fix | array_merge()

The output will be as follows:

array(2) { [0]=> int(1) [1]=> int(2) }
NULL
NULL

It will also generate two warning messages as follows:

Warning: array_merge(): Argument #2 is not an array
Warning: array_merge(): Argument #1 is not an array
View Detials

Q.How can we know the number of days between two given dates using PHP?

Ans.:

Bellow code is used for the same

$date1 = date(‘Y-m-d’);
$date2 = ‘2006-08-15’;
$days = (strtotime($date1) – strtotime($date2)) / (60 * 60 * 24);
echo $days;

 

Q.What will be the output of $a and $b once code bellow is executed?
$a = '1';
$b = &$a;
$b = "2$b";

 

Ans.:

PHP | output of $a and $b once this code $a = '1'; $b = &$a; $b = "2$b" executed;

Both $a and $b will output a string "21".

View Detials

Q.What will be the value of $x when given statement execute?
$x = 3 + "15%" + "$25"?

Ans.:

PHP | Statement $x = 3 + "15%" + "$25"?

The correct answer is 18.

Here’s why:

PHP is a  loosely typed language, therefore it does automatic type conversion based on the context in which a variable or value is being used.

View Detials

Q.How we can check data type of any variable in php ?

Ans.:

PHP | Check data type in php | gettype() | var_dump()

By using gettype() function we can check the data type of a variable.

we can also use var_dump() function for the same.

View Detials

Q.How to convert a string to array ?

Ans.:

PHP | How to convert a string to an array?

string to array

There is a function in PHP called explode() which is used to convert any string to an array. 

View Detials

Q.What is :: operator and in what case we use it in php?

Ans.:

PHP | :: operator | scope resolution operator

This operator is known as the scope resolution operator. And this operator is used to access the static members of the class. To access static members of a class we do not need to create an object/instance of the class.

View Detials

Q.What are data types in php?

Ans.:

PHP | What are data types in PHP?

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String
  • Integer
  • Float (floating-point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Q.What are superglobal/global variables ?

Ans.:

PHP | What are superglobal/global variables?

Superglobal is out of scope limitation. These variables can be accessed from any function, class, or file without doing anything special such as declaring any global variable, etc. These variables store information in an array. Commonly they are used to store and get information from one page to another in an application.

Below is the list of superglobal variables available in PHP:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_GET
  • $_POST
  • $_SESSION
  • $_COOKIE
  • $_FILES
  • $_ENV

Q.What is difference between require() and include() ?

Ans.:

What is difference between require() and include() ?

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

(same for require_once() and include_once())

Q.What is difference between echo and print?

Ans.:

PHP |What is the difference between echo and print?

PHP echo and print both are used to display the output in PHP. Both can be used with or without parenthesis. The differences are small

  • echo has no return value while print has a return value of 1, so it can ve used in expressions.
  • echo can take multiple parameters where as print can take only one argument.
  • echo is marginally faster than print.
View Detials

Q.How to convert an array to string ?

Ans.:

PHP | How to convert an array to a string?

array to string

By using implode function we can convert any array to string.  In implode function first parameter is separator which specifies what to put between the array elements. Default is "" (an empty string)

View Detials

Q.What are scopes of variable in php?

Ans.:

PHP | What are the scopes of variables in PHP?

Scopes define the visibility of a variable. In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be used.

There are three scopes available in PHP

  1. global
  2. local
  3. static
View Detials

Q.What is difference between require() and require_once() ?

Ans.:

PHP | require() and require_once()

Both require() and require_once() is use to include a file but the difference is required_once() function checks if the file already included or not where  require() function does not check.

(same for include_once() and include())