PHP INTERVIEW QUESTION
1) What is PHP?
PHP stands for Hypertext Preprocessor. It is an open-source server-side
scripting language which is widely used for web development. It supports many
databases like MySQL, Oracle, Sybase, Solid, PostgreSQL, generic ODBC etc.
2) What is PEAR in PHP?
PEAR is a framework and repository for reusable PHP components. PEAR
stands for PHP Extension and Application Repository. It contains all types of
PHP code snippets and libraries. It also provides a command line interface to
install "packages" automatically.
3) Explain the difference b/w static and dynamic websites.
On a static website, everyone visiting the site gets the same information because
there is no automatic change to the HTML. Such sites require less time and
effort than others, though they are basic and lack active elements.
With a dynamic website, the server uses scripts (such as PHP) to make the
content as it is requested. It can show content that's made just for you, get
information from databases, and react when people type or click-all of which
makes it great for things like websites, blogs, and online shopping sites.
4) What are the Characteristics of PHP?
PHP is used on the server side to create web applications. Here are the main
features it displays:
o Open-source and free to use.
o You can use it on Windows, Linux, and macOS.
o Its syntax is much like that of C and Perl, meaning programmers who use
them will find it easier to use PHP.
o Can embed directly into HTML.
o Almost all Python frameworks come with support for MySQL,
PostgreSQL, and SQLite.
o Using JavaScript makes websites responsive and popular among
developers.
5) What is the difference between "echo" and "print" in PHP?
Echo can output one or more strings, but print can only output one string and
always returns 1.
Echo is faster than print because it does not return any value.
6) How a variable is declared in PHP?
A PHP variable is the name of the memory location that holds data. It is
temporary storage.
Syntax
1. $variableName=value;
7) What is the difference between $mess9age and $$message?
$(Standard Variable) $$(Reference variable)
$message stores variable data while $$message is used to store variable
variables.
$message stores fixed data, whereas the data stored in $$message may be
changed dynamically.
Ex. $var1 = “name”; // first variable
$$var1 = “ABC”; // variable with value
8) What are the ways to define a constant in PHP?
PHP constants are names or identifiers that can't be changed during the
execution of the script. PHP constants are defined in two ways:
o Using define() function
o Using const() function
9) What are magic constants in PHP?
PHP magic constants are predefined constants that change based on their use.
They start with a double underscore (__) and end with a double underscore (__).
10) How to do single and multi-line comments in PHP?
PHP single-line comment is made in two ways:
o Using // (C++ style single line comment)
o Using # (Unix Shell style single line comment)
PHP multi-line comment is made by enclosing all lines within.
11) What are the different loops in PHP?
The for, while, do-while and foreach loops are used in PHP to execute the
following lines of code repeatedly, depending on certain conditions or a bunch
of elements.
For Loop
PHP for loop can be used to traverse set of code for the specified number of
times.
It should be used if the number of iterations is known otherwise use while loop.
This means for loop is used when you already know how many times you want
to execute a block of code.
It allows users to put all the loop related statements in one place. See in the
syntax given below:
The syntax of PHP for loop is given below:
for(initialization; condition; increment/decrement)
{
//code to be executed
}
While Loop
PHP while loop can be used to traverse set of code like for loop. The while loop
executes a block of code repeatedly until the condition is FALSE. Once the
condition gets FALSE, it exits from the body of loop.
It should be used if the number of iterations is not known.
The while loop is also called an Entry control loop because the condition is
checked before entering the loop body. This means that first the condition is
checked. If the condition is true, the block of code will be executed.
Syntax
while(condition){
//code to be executed
}
Do-While Loop
PHP do-while loop can be used to traverse a set of code like php while loop. It
is guaranteed to execute the code at least one time always because the condition
is checked after executing the code.
In other words, the PHP do-while loop is used to execute a set of code of the
program several times. It is very much similar to the while loop except the
condition check. The main difference between both loops is that while loop
checks the condition at the beginning, whereas do-while loop checks the
condition at the end of the loop.
Syntax
do{
//code to be executed
}while(condition);
For-Each Loop
The foreach loop is used to traverse the array elements. It works only on array
and object. It will issue an error if you try to use it with the variables of different
datatype.
The foreach loop works on elements basis rather than index. It provides an
easiest way to iterate the elements of an array.
In foreach loop, we don't need to increment the value.
Syntax
foreach ($array as $value) {
//code to be executed
}
12) What is the use of the count() function in PHP?
The PHP count() function is used to count the total elements in the array or
something an object.
13) What is the use of the header() function in PHP?
The header() function is used to send a raw HTTP header to a client. It must be
called before sending the actual output. For example, you can't print any HTML
element before using this function.
14) What does isset() function?
The isset() function checks if the variable is defined and not null.
15) Explain PHP parameterised functions.
Parameterised functions allow the same function to respond to different inputs
by getting values passed to them (parameters) when it is called.
Example:
1. function greet($name) {
2. echo "Hello, $name!";
3. }
4. greet("Alice");
Output:
Hello, Alice!
16) What are superglobals in PHP?
Superglobals are always present in PHP and you can use them from any scope.
Examples include:
$_GET: data from URL parameters
$_POST: data from form submissions
$_SESSION: session variables
$_COOKIE: cookies
$_SERVER: server and execution environment info
17) What is the array in PHP?
An array is used to store multiple values in a single value. In PHP, it orders
maps of pairs of keys and values. It saves the collection of the data type.
18) How many types of arrays are there in PHP?
There are three types of arrays in PHP:
1. Indexed array:an array with a numeric key.
2. Associative array:an array where each key has its specific value.
3. Multidimensional array:an array containing one or more arrays within
itself.
19) What is the difference between an indexed and associative array?
The indexed array holds elements in an indexed form, which is represented by a
number starting from 0 and incremented by 1. For example:
1. $season=<strong>array</strong>("summer", "winter", "spring", "autumn
");
The associative array holds elements with names. For example:
1. $salary=<strong>array</
strong>("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
20) Explain some of the PHP string functions.
The string functions of PHP are as follows:
strtolower(): Converts all characters in a string to lowercase.
strtoupper(): Converts all characters in a string to uppercase.
ucfirst(): Capitalises the first character of a string.
lcfirst(): Converts the first character of a string to lowercase.
ucwords(): Capitalises the first character of each word in a string.
strrev(): Reverses the string.
strlen(): Returns the length of the string (number of characters).
21) What are the ways to include files in PHP?
PHP allows you to include files so that page content can be reused again. There
are two ways to add the file in PHP.
1. include
2. require
22) Differentiate between require and include?
require and include both are used to include a file, but if data is not
found, include sends a warning, whereas require sends a fatal error.
23) Differentiate between require and include?
require and include both are used to include a file, but if data is not
found, include sends a warning, whereas require sends a fatal error.
24) What is a session?
PHP Engine creates a logical object to preserve data across subsequent HTTP
requests, which is known as a session.
Sessions generally store temporary data to allow multiple PHP pages to offer a
complete functional transaction for the same user.
Simply, it maintains the data of a user (browser).
25) What is $_SESSION in PHP?
$_SESSION in PHP allows for data to be stored across various pages by one
user. Since session data is held on the server, it is more secure than cookies.
You should call session_start() at the beginning of your script to use
$_SESSION, as follows:
Example:
1. session_start();
2. $_SESSION["username"] = "prachetyerr"
3. echo $_SESSION["username"];
Output:
prachetyerr
26) What is the difference between a session and a cookie?
The main difference between session and cookie is that cookies are stored on
the user's computer in the text file format while sessions are stored on the
server side.
Cookies can't hold multiple variables; on the other hand, a Session can hold
multiple variables.
You can manually set an expiry for a cookie, while the session only remains
active as long as the browser is open.
27) What are the different types of errors in PHP?
There are three types of errors in PHP.
1. Notices: These are non-critical errors. These errors are not displayed to
the users.
2. Warnings: These are more serious errors, but they do not result in script
termination. By default, these errors are displayed to the user.
3. Fatal Errors: These are the most critical errors. These errors may be
caused by to immediate termination of the script.
28) Explain PHP explode() function.
The explode() function in PHP allows splitting a string into smaller segments
delimited by a character. For example, explode(",", "apple, banana, orange")
will return an array with the elements ["apple", "banana", "orange"].
29) Explain PHP split() function.
PHP's split() function uses a regular expression to break a string into an array.
Yet, as of PHP 5.3.0, it is no longer supported and is not available in PHP 7.0.
You should prefer using preg_split() instead.
30) What is Cookies? How to create cookies in PHP?
A cookie is used to identify a user. A cookie is a little record that the server
installs on the client's Computer. Each time a similar PC asks for a page with a
program, it will send the cookie as well. With PHP, you can both make and
recover cookie value.
You can create a cookie in PHP using the setcookie() function.
Syntax:
setcookie(name, value, expire, path, domain, secure, httponly);