PHP Interview Questions & Answers for Freshers

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 are the common uses of PHP?

  • It performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • It can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.
  • You can add, delete, modify elements within your database with the help of PHP.
  • Access cookies variables and set cookies.
  • Using PHP, you can restrict users to access some pages of your website and also encrypt data.

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

4. 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!”

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

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

  • Variables can be declared before the value assignment.
  • A variable value assignment happens using the ‘=’ operator.
  • Every variable in PHP is denoted with a $ (dollar) sign.
  • The value of a variable depends on its latest assigned value.
  • PHP variables are not intrinsic. There is no explicit declaration.

6. What is the name of scripting engine in PHP?

The scripting engine that powers PHP is called Zend Engine 2.

7. What are the popular Content Management Systems (CMS) in PHP?

  • WordPress: WordPress is a free and open-source content management system (CMS) based on PHP & MySQL. It includes a plug-in architecture and template system. It is mostly connected with blogging but supports another kind of web content, containing more traditional mailing lists and forums, media displays, and online stores.
  • Joomla: Joomla is a free and open-source content management system (CMS) for distributing web content, created by Open Source Matters, Inc. It is based on a model-view-controller web application framework that can be used independently of the CMS.
  • Magento: Magento is an open source E-trade programming, made by Varien Inc., which is valuable for online business. It has a flexible measured design and is versatile with many control alternatives that are useful for clients. Magento utilizes E-trade stage which offers organization extreme E-business arrangements and extensive support network.
  • Drupal: Drupal is a CMS platform developed in PHP and distributed under the GNU (General Public License).

8. What are the popular frameworks in PHP?

  • CakePHP
  • CodeIgniter
  • Yii 2
  • Symfony
  • Zend Framework etc.

9. List some of the features of PHP7

  • Scalar type declarations
  • Return type declarations
  • Null coalescing operator (??)
  • Spaceship operator
  • Constant arrays using define()
  • Anonymous classes
  • Closure::call method
  • Group use declaration
  • Generator return expressions
  • Generator delegation
  • Space ship operator

10. What is NULL?

NULL is a special data type which can have only 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 capitalized by convention but actually it is case insensitive. So,you can also write it as :

$var = null;

A variable that has been assigned the NULL value, consists of the following properties:

  • It evaluates to FALSE in a Boolean context.
  • It returns FALSE when tested with IsSet() function.

11. What is the purpose of constant() function?

The constant() function will return the value of the constant. This is useful when you want to retrieve value of a constant, but you do not know its name, i.e., it is stored in a variable or returned by a function. For example –

<?php 
define("MINSIZE", 50); 
echo MINSIZE; 
echo constant("MINSIZE"); // same thing as the previous line 
?>

12. Name some of the constants in PHP and their purpose.

_LINE_ – It represents the current line number of the file.
_FILE_ – It represents the full path and filename of the file. If used inside an include,the name of the included file is returned.
_FUNCTION_ – It represents the function name.
_CLASS_ – It returns the class name as it was declared.
_METHOD_ – It represents the class method name.

13. How can PHP and HTML interact?

It is possible to generate HTML through PHP scripts, and it is possible to pass pieces of information from HTML to PHP. PHP is a server side language and HTML is a client side language so PHP executes on server side and gets its results as strings, arrays, objects and then we use them to display its values in HTML.

14. What are constructor and destructor in PHP?

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

Here is an example of constructor and destructor in PHP:

<?php 
class Foo { 
private $name; 
private $link; 
public function __construct($name) { 
$this->;
name = $name; 
} 
public function setLink(Foo $link){ 
$this->;
link = $link; 
} 
public function __destruct() { 
echo 'Destroying: ', $this->name, PHP_EOL; 
} 
}
?>

15. What are include() and require() functions?

The Include() function is used to put data of one PHP file into another PHP file. If errors occur then the include() function produces a warning but does not stop the execution of the script and it will continue to execute.

The Require() function is also used to put data of one PHP file to another PHP file. If there are any errors then the require() function produces a warning and a fatal error and stops the execution of the script.

16. What are the different types of Array in PHP?

There are 3 types of Arrays in PHP:

  • Indexed Array – An array with a numeric index is known as the indexed array. Values are stored and accessed in linear fashion.
  • Associative Array – An array with strings as index is known as the associative array. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional Array – An array containing one or more arrays is known as multidimensional array. The values are accessed using multiple indices.

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

18. What is the difference between $message and $$message?

$message stores variable data while $$message is used to store variable of variables.

$message stores fixed data whereas the data stored in $$message may be changed dynamically.

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

20. Name some of the PHP string functions?

There are many array functions in PHP:

  • strtolower()
  • strtoupper()
  • ucfirst()
  • lcfirst()
  • ucwords()
  • strrev()
  • strlen()