Blog

PHP Interview Questions For Freshers

Image

This article will show you the basic interview questions asked in any interview. Basically questions start with some basic concepts of the language and later they continue based on the further discussion.

PHP stands for PHP: Hypertext Preprocessor is a widely used open source server-side scripting language, especially suitable for building dynamic web pages and mobile APIs.

Let's see the list of top Basic Level PHP Interview Question:

Q1: Differentiate Between Variables And Constants In PHP
VariablesConstants
You can change the value of the variable during execution.You cannot change the constant value while the script is running.
Variables must use the $ sign as required. at the start.The dollar sign ($) is not required before using the constant.
You can define variables with a simple assignment.The constant cannot be defined by a simple assignment. They are defined by the define () function.
The default scope is the current access scope.Constants can be accessed from anywhere without scope rules.
Q2: What Is A Session In PHP?

A PHP session is a way of storing information to be used on multiple pages throughout your website. The information is not stored on the user’s computer, unlike cookies. In a temporary directory on the server, a file will be created by the session where registered session variables and their values are stored. This information will be available on all pages of the website during this visit.

When you're working with an app, you open it, make a few changes, then close it. It looks a lot like a session. The computer knows who you are. It knows when you start and end the application.

But on the Internet, the web server doesn't know who you are or what you're doing because the HTTP address doesn't hold any state. This problem is solved by using session variables by storing user information for use across multiple sites (e.g. username, preferred color, etc.).

By default, session variables will last until the user closes the browser.

Therefore, session variables contain information about a user and are available to all pages of an application.

<?php
    session_start();
    session_destroy();
?>
Q3: What Does PEAR Stands For?

PEAR stands for “PHP Extension and Application Repository”. PEAR is a framework and repository for all of the reusable PHP components.

PEAR offers a higher level of programming for web developers. It contains all kinds of PHP scripts and libraries. It also provides you with a command line interface to automatically install packages.

Q4: What Is The Meaning Of ‘Escaping To PHP’?

PHP crawlers need a way to differentiate PHP code from other elements on the page. The mechanism for doing this is called "escape to PHP". Escaping a string means reducing ambiguity in the quotes used in that string

Q5: What Are The Different Types Of Variables Present In PHP?
  • Integers: Integers are whole numbers without a floating-point. Ex: 1253.
  • Doubles: Doubles are floating-point numbers. Ex: 7.876
  • Booleans: It represents two logical states- true or false.
  • NULL: NULL is a special type that only has one value, NULL. When no value is assigned to a variable, it can be assigned with NULL.
  • Arrays: Array is a named and ordered collection of similar type of data. Ex: $colors = array("red", "yellow", "blue");
  • Strings: Strings are a sequence of characters. Ex: “Hello InterviewBit!”
  • Resources: Resources are special variables that consist of references to resources external to PHP(such as database connections).
  • Objects: An instance of classes containing data and functions. Ex: $mango = new Fruit();
Q6: What Are The Rules For Naming A PHP Variable?

The following two rules are needed to be followed while naming a PHP variable:

  • A variable must begin with a dollar sign, followed by the variable's name. Example: $price = 100; where price is a variable name.
  • Variable names must start with a letter or an underscore.
  • A variable name can include letters, numbers, or underscores. But you cannot use characters like +, -, %, & etc.
  • PHP variable names cannot contain spaces.
  • PHP variables are case sensitive. Therefore, both $NAME and $name are treated as different variables.
Q7: What Is The Difference Between “Echo” And “Print” In PHP?
echoprint
echo can output one or more stringsprint can only output a string and always returns 1
echo is faster than print because it doesn't return any valueprint is slower than echo
If you want to pass more than one parameter to echo, parentheses must be usedThe use of parentheses is optional with argument lists
Q8: What Is NULL?

NULL is a special data type that can only have one value. A variable of data type NULL is a variable that has no value assigned to it. It can be assigned as follows:

$var = NULL;

The special constant NULL is uppercase by convention, but is actually case-insensitive. So you can also write it as:

$var = null;

A variable that has been assigned a NULL value includes the following properties:

  • It evaluates to FALSE in a Boolean context.
  • It returns FALSE when tested with the IsSet() function.
Q9: What Is The Purpose Of @ In PHP?

In PHP, @ is used to suppress error messages. If a runtime error occurs on a line with the @ symbol at the beginning, the error will be handled by PHP.

Q10: What Are The Different Types Of Array In PHP?

There are 3 main types of arrays that are used in PHP:

Indexed Array

Arrays with numeric keys are called indexed arrays. The values are stored and accessed in a linear order.

Associative Array

Arrays with strings to index the elements are called associative arrays. Element values are stored along with the key values rather than in strict linear index order.

Multidimensional Array

An array that contains one or more arrays in itself is called a multidimensional array. The values are accessible using a number of indices.

Q11: Differentiate Between GET And POST
GETPOST
The GET method is used to request data from a specific resource.POST is used to send data to the server as a packet in separate communication with the processing script.
The data is sent as a URL parameter, which is a string of name-value pairs separated by an ampersand (&).Data sent via POST method will not be seen in the URL
The GET method cannot be used to send binary data such as images or Word documents.The POST method can be used to send ASCII data as well as binary data such as images and Word documents.
This method should not be used if you have sensitive information such as passwords to send to the server.It is possible to send sensitive information using this method.
It can be used to submit a form where the user can bookmark the result.Submissions by form with POST cannot be bookmarked.
You can only use this method for unsafe data.Data sent using this method is SECURE.
The GET method is not more secure because the parameters can be stored in the web server logs or in the browser history.The POST method is more secure than GET because the parameters are not stored in the web server logs or in the browser history.
Q12: Name Some Of The Constants In PHP And Their Purpose.
  1. _LINE_ — It represents the current line number of the file.
  2. _FILE_ — It represents the full path and filename of the file. If used inside an include, the name of the included file is returned.
  3. _FUNCTION_ — It represents the function name.
  4. _CLASS_ — It returns the class name as it was declared.
  5. _METHOD_ — It represents the class method name.
Q13: What Is The Purpose Of Break And Continue Statement?

Break — It terminates the for loop or switch statement and transfers execution to the statement immediately following the for loop or switch.

Continue — It causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Q14: What Is The Meaning Of A Final Class And A Final Method?

The final keyword in the method declaration indicates that the method cannot be replaced by subclasses. A class declared final cannot be subclassed. This is especially useful when we are creating an immutable class like the String class. Properties cannot be declared final, only classes and methods can be declared final.

Q15: How Can You Compare Objects In PHP?

We use the operator ‘==’ to test if two objects are instanced from the same class and have same attributes and equal values. We can also check if two objects refer to the same instance of the same class using the identity operator `===`.

Q16: What Are Constructor And Destructor In PHP?

PHP constructor and destructor are special type functions that are called automatically when a PHP class object is created and destroyed. The constructor is more useful in two ways because it allows you to pass parameters when creating a new object, which can then be used to initialize variables on the object.

Q17: What Are Include() And Require() Functions?

The include() function is used to include data from one PHP file into another PHP file. If any error occurs, the include() function generates a warning but does not stop the script execution and it will continue running.

The require() function is also used to put data from one PHP file into another PHP file. If there are any errors, the require() function will generate a warning and a fatal error and stop the execution of the script.

Q18: What Is The Main Difference Between Require() And Require_once()?

The require() includes and evaluates a specific file, while require_once() does that only if it has not been included before. The require_once() statement can be used to include a php file in another, when you may need to include the file being called multiple times. Therefore, request_once() is recommended to use when you want to include a file containing multiple functions.

Q19: Types Of Loops In PHP

Loops are used to execute the same block of code over and over again, as long as a certain condition is met. The basic idea behind a loop is to automate repetitive tasks in a program to save time and effort. PHP supports four different types of loops.

for — loops through a block of code until the counter reaches a specified number.

for(initialization; condition; increment){
    // Code to be executed
}

foreach — loops through a block of code for each element in an array.

while — loops through a block of code as long as the specified condition is true.

while(condition){
    // Code to be executed
}

dowhile — the block of code is executed once and then the condition is evaluated. If the condition is true, the statement is repeated as long as the specified condition is true.

Q20: What Is The Use Of Session And Cookies In PHP?

session is a global variable stored on the server. Each session is assigned a unique ID that is used to retrieve the stored value. Sessions are used to securely store information such as user IDs on the server. Sessions can store a relatively large amount of data compared to cookies. The session value is automatically deleted when you close the browser.

A cookie is a small text file that is stored on the user's computer. The maximum cookie file size is 4KB. Sessions are cookie-dependent, but cookies are session-independent. Cookies expire after the expiration date set by the user. Cookies are not secure because the data is stored in a text file.

Q21: What Is Overloading And Overriding In PHP?

Overloading define functions with similar signatures, but with different parameters. Overriding is only suitable for derived classes, where the parent class has defined a method and the derived class wants to override that method. In PHP, you can only overload methods with the magic __call method.

Q22: What Is The Use Of Callback In PHP?

Callbacks in PHP are functions that can be called dynamically by PHP. They are used by native functions like array_map, usort, preg_replace_callback, etc. A callback function is a function that you create yourself and then pass to another function as an argument. Once it has access to your callback function, then the get function can call it whenever it needs to.

Q23: What Is The Use Of 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.

Q24: Some Of The PHP Array Functions?

There are many array functions in PHP:

  • array_search()
  • implode() and explode()
  • in_array()
  • array_search()
  • array_intersect()
  • array_reverse()
  • array_chunk()
  • count()
  • sort()