Interview Questions




Most Essential And Frequently Asked Interview
Questions And Answer

PHP

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.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.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.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.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.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.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.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 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.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.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 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.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.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.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 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.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 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.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 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 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 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.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.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.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 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.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 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 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.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.