Categories

Top 50 PHP Interview Questions and Answers

PHP finds itself among the top languages because of its ability to have a large impact on the outcome with very little code. This amount of efficiency has been the requirement for the past couple of years in the industry. Now, seeing this, companies across the globe are investing a good amount of money in hiring proficient PHP developers to fit these shoes and work effectively. These top core PHP interview Questions will give you the edge that is required to approach the questions effectively and answer them in a concise manner.

1. What is PHP most used for?

PHP has a plethora of uses for developers. Following are some of the most widely used concepts that PHP offers:

1) With PHP, it becomes very easy to provide restricted access to the required content of the website.

2) It allows users to access individual cookies and set them as per requirement.

3) Database manipulation operations, such as addition, deletion, and modification, can be done easily.

4) Form handling, alongside features that involve file handling concepts and email integration, is used widely.

5) The system module allows users to perform a variety of system functions such as open, read, write, etc.

2. Is PHP a case-sensitive scripting language?

The answer to this is both yes and no. Variables and their declaration in PHP are completely case sensitive while function names are not.

For example, user-defined functions in PHP can be defined in uppercase but later referred to in lowercase, and it would still function normally.

3.How is a PHP script executed?

PHP scripts can be easily executed from the command-line interface (CLI). The syntax is as follows:

php filename.php


Here, filename refers to the file that contains scripts. The extension .php is needed alongside the filename.

4. Is PHP a case-sensitive scripting language?

The answer to this is both yes and no. Variables and their declaration in PHP are completely case sensitive while function names are not.

For example, user-defined functions in PHP can be defined in uppercase but later referred to in lowercase, and it would still function normally.

5. What is the meaning of PEAR in PHP?

PEAR stands for PHP Extension and Application Repository. It is one of the frameworks and acting repositories that host all of the reusable PHP components. Alongside containing some of the PHP libraries, it also provides you with a simple interface to automatically install packages.

6. How are constants defined in PHP?

Constants can be defined easily in PHP by making use of the define() function. This function is used to define and pull out the values of the constants easily.

Constants, as the name suggests, cannot be changed after definition. They do not require the PHP syntax of starting with the conventional $ sign.

7. What are the main characteristics of a PHP variable?

Following are some of the most important aspects of the usage of variables in PHP:

1) Variables can be declared before the value assignment.

2) A variable value assignment happens using the ‘=’ operator.

3) Every variable in PHP is denoted with a $ (dollar) sign.

4) The value of a variable depends on its latest assigned value.

5) PHP variables are not intrinsic. There is no explicit declaration.

8. What are the variable-naming rules you should follow in PHP?

There are two main rules that you have to follow when naming a variable in PHP. They are as follows:

1) Variables can only begin with letters or underscores.


2) Special characters such as +, %, -, &, etc. cannot be used.

9. What are the types of variables present in PHP?

There are eight primary data types in PHP as shown below:

Array:
A named and ordered collection of data

Boolean:
A logical value (True or False)

Double:
Floating point numbers such as 5.1525

Integer:
Whole numbers without a floating point

Object:
An instance of classes, containing data and functions

NULL:
A special data type, supporting only the NULL data

Resource:
Special variables that hold references to external resources

String:
A sequence of characters such as, “Hello learners!”

10. What is NULL in PHP?

NULL is a special data type in PHP used to denote the presence of only one value, NULL. You cannot assign any other value to it.

NULL is not case sensitive in PHP and can be declared in two ways as shown below:

$var = NULL:

or

$var = null;

11. How does JavaScript interact with PHP?

JavaScript is a client-side programming language, while PHP is a server-side scripting language. PHP has the ability to generate JavaScript variables, and this can be executed easily in the browser, thereby making it possible to pass variables to PHP using a simple URL.

12. What is the use of the final class and the final method in PHP?

The ‘final’ keyword, if present in a declaration, denotes that the current method does not support overriding by other classes. This is used when there is a requirement to create an immutable class.

Note:
Properties cannot be declared as final. It is only methods and classes that get to be final.

13. What are some of the popular frameworks in PHP?

There are many frameworks in PHP that are known for their usage. Following are some of them:

1) CodeIgniter

2) CakePHP

3) Laravel

4) Zend

5) Phalcon

6) Yii 2

14. What is the meaning of break and continue statements in PHP?

Break:
This statement is used in a looping construct to terminate the execution of the iteration and to immediately execute the next snippet of code outside the block of the looping construct.

Continue:
This statement is used to skip the current iteration of the loop and continue to execute the next iteration until the looping construct is exited.

15. How are two objects compared in PHP?

PHP provides you with the ‘==’ operator, which is used to compare two objects at a time. This is used to check if there is a common presence of attributes and values between the objects in comparison.

The ‘===’ operator is also used to compare if both objects in consideration are referencing to the same class.

16. What does the phrase ‘PHP escape’ mean?

PHP escape is a mechanism that is used to tell the PHP parser that certain code elements are different from PHP code. This provides the basic means to differentiate a piece of PHP code from the other aspects of the program.

17. What are the various constants predefined in PHP?

PHP consists of many constants, and following are some of the widely used ones:

_METHOD_:
Represents the class name

_CLASS_:
Returns the class name

_FUNCTION_:
Denotes the function name

_LINE_:
Denotes the working line number

_FILE_:
Represents the path and the file name

18. What is the use of the constant() function in PHP?

The constant() function is used to retrieve the values predefined in a constant variable. It is used especially when you do not know the name of the variable.

19. Differentiate between variables and constants in PHP.

Variable

Constant

Variables can have changed paths

Constants cannot be changed

The default scope is the current access scope

Constants can be accessed throughout without any scoping rules

The $ assignment is used for definition

Constants are defined using the define() function

Compulsory usage of the $ sign at the start

No need for the $ sign for constants

20. Differentiate between PHP4 and PHP5.

PHP4

PHP5

No support for static methods

Allows the usage of static methods

Abstract classes cannot be declared

Abstract classes can be declared

The method of call-by-value is used

The method of call-by-reference is used

Constructors can have class names

Constructors have separate names
 

Intermediate PHP Interview Questions

21. How does JavaScript interact with PHP?

JavaScript is a client-side programming language, while PHP is a server-side scripting language. PHP has the ability to generate JavaScript variables, and this can be executed easily in the browser, thereby making it possible to pass variables to PHP using a simple URL.

22. What are the types of arrays supported by PHP?

There are three main types of arrays that are used in PHP.

Indexed arrays:
These are arrays that contain numerical data. Data access and storage are linear.

Associative arrays:
There are arrays that contain strings for indexing elements.

Multidimensional arrays:
These are arrays that contain more than one index and dimension.

23. Does PHP interact with HTML?

Yes, HTML and PHP interaction is the core of what makes PHP what it is. PHP scripts have the ability to generate HTML mode and move around information very easily.

PHP is a server-side scripting language, while HTML is a client-side language. This interaction helps bridge the gaps and use the best of both languages.

24. How does the ‘foreach’ loop work in PHP?

The foreach statement is a looping construct used in PHP to iterate and loop through the array data type. The working of foreach is simple; with every single pass of the value, elements get assigned a value and pointers are incremented. This process is repeated until the end of the array.
foreach(array)
{
Code inside the loop;
}

25. Differentiate between require() and require_once() functions.

JavaScript is a client-side programming language, while PHP is a server-side scripting language. PHP has the ability to generate JavaScript variables, and this can be executed easily in the browser, thereby making it possible to pass variables to PHP using a simple URL.

require()

require_once()

The inclusion and evaluation of files

Includes files if they are not included before

Preferred for files with fewer functions

Preferred when there are a lot of functions
 

26. What are the data types present in PHP?

PHP supports three types of data handling, and they are as shown in the following table:

Scalar Data Types

Compound Data Types

Special Data Types

Boolean

Array

NULL

Integer<>

Object

Resource

Float

String

27. What are some of the top Content Management Systems (CMS) used in PHP?

There are many CMS that are used in PHP. The popular ones are as mentioned below:

1) WordPress

2) Joomla

3) Magneto

4) Drupal

28. What is the use of constructors and destructors in PHP?

Yes, it is possible to have an infinite execution time in PHP for a script by adding the set_time_limit(0) function to the beginning of a script.

This can also be executed in the php.ini file if not at the beginning of the script.

29. Explain the importance of Parser in PHP?

A PHP parser is software that converts source code into the code that computer can understand. This means whatever set of instructions we give in the form of PHP code is converted into a machine-readable format by the parser.

You can parse PHP code with PHP using the token_get_all() function.

30. How are comments used in PHP?

There are two ways to use comments in PHP. They are single-line comments and multi-line comments.

Single-line comments can be used using the conventional ‘#’ sign.

Example:
<?php
# This is a comment
echo "Single-line comment";
?>
Multi-line comments can be denoted using ‘/* */’ in PHP.
<?php
/*
This is
a
Multi-line
Comment
In PHP;
*/
echo "Multi-line comment";
?>

31. What are the different types of PHP errors?

There are three main types of errors in PHP. They are as follows:

Notice:
A notice is a non-critical error that is not displayed to the user.

Warning:
A warning is an error that is displayed to the user while the script is running.

Fatal error:
This is the most critical type of error. A fatal error will cause immediate termination of the script.

32. Can a form be submitted in PHP without making use of a submit button?

Yes, a form can be submitted without the explicit use of a button. This is done by making use of the JavaScript submit() function easily.

33. Is typecasting supported in PHP?

Yes, typecasting is supported by PHP and can be done very easily. Following are the types that can be cast in PHP:

(int), (integer):
Cast to integer

(bool), (boolean):
Cast to boolean

(float), (double), (real):
Cast to float

(string):
Cast to string

(array):
Cast to array

(object):
Cast to object

34. What are sessions and cookies in PHP?

Sessions are global variables that are stored on the server in the architecture. Every single session is tagged with a unique server ID that is later used to work with the storage and retrieval of values.

Cookies are entities used to identify unique users in the architecture. It is a small file that the server plants into the client system. This is done to get useful information from the client for the development of various aspects of the server.

35. What is the most used method for hashing passwords in PHP?

The crypt() function is widely used for this functionality as it provides a large amount of hashing algorithms that can be used. These algorithms include md5, sha1 or sha256.

Advanced PHP Interview Questions for Experienced and Professionals

36. Differentiate between an indexed array and an associative array.

Indexed arrays have elements that contain a numerical index value.

Example: $color=array("red","green","blue");

Here, red is at index 0, green at 1, and blue at 2. Associative arrays, on the other hand, hold elements with string indices as shown below:

Example: $salary=array("Jacob"=>"20000","John"=>"44000","Josh"=>"60000");

37. What is the difference between ASP.NET and PHP?

ASP.NET

PHP

A programming framework

A scripting language

Compiled and executed

Interpreted mode of execution

Designed for use on Windows

Platform independent

38. Differentiate between compile-time exception and runtime exception in PHP.

As the name suggests, if there is an occurrence of any sort of exception while the script is being compiled, it is called a compile-time exception. The FileNotFoundException is a good example of a compile-time exception.

An exception that interrupts the script while running is called a runtime exception. The ArrayIndexOutOfBoundException is an example of a runtime exception.

39. What are the steps to create a new database using MySQL and PHP?

There are four basic steps that are used to create a new MySQL database in PHP. They are as follows:

1) First, a connection is established to the MySQL server using the PHP script.

2) Second, the connection is validated. If the connection is successful, then you can write a sample query to verify.

3) Queries that create the database are input and later stored into a string variable.

4) Then, the created queries are executed one after the other.

40. How is a URL connected to PHP?

Any URL can be connected to PHP easily by making use of the library called cURL. This comes as a default library with the standard installation of PHP.

The term ‘cURL’ stands for client-side URL, allowing users to connect to a URL and pick up information from that page to display.

41. What is the meaning of type hinting in PHP?

Type hinting is used in PHP when there is a requirement to explicitly define the data type of an argument when passing it through a function.

When this function is first called, PHP will run a quick check to analyze the presence of all the data types that are specified. If it is different, then the runtime will stop as an exception will be raised.

42. Do you have any certification to boost your candidature for this PHP Developer role?

With this question, the interviewer is trying to assess if you have any exposure to real-time projects and hands-on experience. This is usually provided by a good certification program, and this gives an impression to the interviewer that you are serious about the career path you are aspiring for. If you do have any relevant experience, make sure to explain about what you learned and implemented during the certification course.

43. What is Zend Engine?

Zend Engine is used internally by PHP as a compiler and runtime engine. PHP Scripts are loaded into memory and compiled into Zend OPCodes.

These OPCodes are executed and the HTML generated is sent to the client.

The Zend Engine provides memory and resource management and other standard services for the PHP language. Its performance, reliability, and extensibility have played a significant role in PHP’s increasing popularity.

44. What are the new features introduced in PHP7?

1) Zend Engine 3 performance improvements and 64-bit integer support on Windows

2) Uniform variable syntax

3) AST-based compilation process

4) Added Closure::call()

5) Bitwise shift consistency across platforms

6) (Null coalesce) operator

7) Unicode codepoint escape syntax

8) Return type declarations

9) Scalar type (integer, float, string, and Boolean) declarations

45. What library is used for PDF in PHP?

The PDF functions in PHP can create PDF files using PDFlib version 6. PDFlib offers an object-oriented API for PHP5 in addition to the function-oriented API for PHP4. There is also the » Panda module.

FPDF is a PHP class, which allows generating PDF files with pure PHP (without using PDFlib). F from FPDF stands for Free: we may use it for any requirement and modify it to suit our needs. FPDF requires no extension (except zlib to activate compression and GD for GIF support) and works with PHP4 and PHP5.

46. What are magic methods?

Magic methods are member functions that are available to all the instances of a class. Magic methods always start with ‘__’, for example, __construct(). All magic methods need to be declared as public.

To use a method, it should be defined within the class or the program scope. Various magic methods used in PHP5 are:

1) __construct()

2) __destruct()

3) __set()

4) __get()

5) __call()

6) __toString()

7) __sleep()

8) __wakeup()

9) __isset()

10) __unset()

11) __autoload()

12) __clone()

47. What is meant by PEAR in PHP?

PEAR is an acronym for PHP Extension and Application Repository. The purpose of PEAR is to provide:

1) A structured library of open-sourced code for PHP users

2) A system for code distribution and package maintenance

3) A standard style for writing code in PHP

4) PHP Foundation Classes (PFC)

5) PHP Extension Community Library (PECL)

6) A website, mailing lists, and download mirrors to support the PHP/PEAR community

48. What is Memcache?

Memcache is a technology that caches objects in memory such that a web application can get to them really fast. It is used by sites, such as Digg, Facebook, and NowPublic, and is widely recognized as an essential ingredient in scaling any LAMP.

49. How can we execute a PHP script using a command line?

We just have to run the PHP CLI (Command-line Interface) program and provide the PHP script file name as the command-line argument, for example, php myScript.php, assuming php as the command to invoke the CLI program.

We have to keep in mind that if our PHP script is written for the Web CGI interface, it may not execute properly in the command-line environment.

50. What is Smarty?

Smarty is a template engine written in PHP. Typically, these templates will include variables—like {$variable}—and a range of logical and loop operators to allow adaptability within the templates.