This blog lists down the most relevant 30 basic PHP Interview questions. Questions in this blog are easy as per difficulty but have a high chance of being asked as introductory PHP Interview questions. These are questions related to basic concepts of the PHP language.
So, let’s begin to go through the questions one by one.
PHP Interview Questions
- What does PHP stand for?
PHP stands for Hypertext PreProcessor.
- What is PHP?
PHP is a scripting language used for web development. It is an open-source, interpreted server-side scripting language and supports object-oriented programming as well.
- Who created PHP and when?
Rasmus Lerdorf created PHP in 1994 and launched it commercially in 1995.
- How are variables created in PHP?
Variables in PHP always start with the $ sign, followed by the name of the variable. In PHP, variables are created by assigning a value to them. The feature to declare a variable without assigning any value is not available in PHP.
- What is a reference variable in PHP?
A reference variable can store the data about some other variable, and it is created using the $$ symbol before its name. The data stored in it will change if we change the variable’s value whose data is stored in it.
- What are the types of variables in PHP?
There are 8 types of variables in PHP viz. integers, doubles, strings, null, boolean, array, objects, resources.
- What are the restrictions on naming a PHP variable?
The variable name in PHP must begin with an underscore character or a letter. Other than that, it can contain letters, numbers, and underscores. The characters like +,-,(,),&, etc., can’t be included in the name.
- What are constants in PHP?
The identifiers which, once used, can not be changed during the execution of the code are called constants. They follow similar nomenclature restrictions as PHP variables.
- What are two methods to define PHP constants?
The two methods to define constants in PHP are the define() function and the const keyword. The syntax for declaring constant using define method –
define(name, value, case-insensitive)
The syntax for declaring constant using const keyword –
const constantvr = 5;
- What are the magic constants in PHP?
These are predefined constants in the PHP which change their value depending on the context in which they are used. Their names start with double underscore(__) and end with double underscore. Currently PHP has 9 magic constants viz. __LINE__,__FILE__,__DIR__,__FUNCTION__,__CLASS__,__TRAIT__,__METHOD__,__NAMESPACE__,ClassName::class.
- Tell me about the function of __LINE__ and __FILE__?
- __LINE__ – It is used to represent the current line of the file being processed.
- __FILE__ – It is used to represent the file name and full path to the file. It also returns the name of the included file if used inside an include.
- What is pear in PHP?
Pear is an abbreviation of “PHP Extension and Application Repository.” It is a library of PHP components that are extensively reused. It also provides a command-line interface for package installation.
- Which scripting engine is used by PHP?
Zend engine 2 is used as a scripting engine in PHP.
- What are the differences between static and dynamic websites?
In static websites, the content is fixed as it can not be modified after running the script, whereas in dynamic websites, we can modify content as and when required.
- Why is PHP a partially case-sensitive language?
PHP is a partial case-sensitive language because it does not strictly follow the case-sensitivity as variable names are completely case sensitive. In contrast, the function names are not case-sensitive.
- How can you display text on the output in PHP?
We can use either echo or print to display text on the output.
- What is the echo in PHP?
Echo is a text display method in PHP, it can output multiple strings and returns no value. The syntax to use echo is as follows-
<!DOCTYPE html> <html> <body> <?PHP echo "Coding Ninjas<br>"; echo "To ", "display text ", "in fragments. "; ?> </body> </html>
- What is print in PHP?
print is a text display method in PHP, it outputs only one string and always return 1, the syntax used for this is as follows-
<!DOCTYPE html> <html> <body> <?PHP print "Coding Ninjas<br>"; ?> </body> </html>
- What are the advantages of PHP?
There are several advantages of using PHP over other languages., It is a server-side scripting language that is suitable for creating dynamic web pages. It is an easy to integrate language. That is, it can be easily integrated with other technologies.
- What are the disadvantages of PHP?
There are several disadvantages of using PHP over other languages. It is less secure being open source, not suitable for bulky web apps and lacks debugging tools.
- What is ‘escaping to PHP’?
PHP parsing engine’s mechanism to differentiate PHP code from other elements is called “escaping to PHP.”
- What is the additional feature in PHP 5 from PHP 4?
The major additional features in PHP5 include improved object-oriented programming and the ability to pass values by reference.
- Why is PHP preferred to develop content management systems? Name some of them?
PHP is preferred to develop a content management system because it supports dynamic web pages. Content management systems require this function to let their users edit, create and update the content. Some of the most known content management systems in PHP are – WordPress, Drupal, Magento, Joomla, etc.
- PHP resembles which languages?
PHP structure resembles a lot with that of Perl and c.
- What is a final method or final class in PHP?
The final keyword helps us restrict methods and classes from being overwritten by subclasses, i.e., a final method can not be overridden by the subclasses, and a final class can not be subclassed.
- How can we make single-line comments in PHP?
We can make single-line comments in PHP using any of these two methods-
Using // before the line to be commented.
Using # before the line to be commented.
- How can we make multi-line comments in PHP?
To make a multi-line comment, we use /* before starting the comment and */ after the ending of the comment.
- How many types of an array are there in PHP?
PHP has three types of array-
- Indexed array- Arrays having numeric keys are called indexed arrays.
- Multidimensional array- Arrays having one or more than one array in themselves is called a multidimensional array.
- Associative array- Array having a specific value for each key is called an associative array.
- How can we set an infinite execution time for a PHP script?
We can do this by putting the set_time_limit() function at the beginning of the script. This function takes the values in seconds as a parameter and returns either true or false. It returns an error if the time limit is reached.
- What does the split function do?
The split function breaks the string into fragments based on the pattern given to it as a parameter. It returns an array of fragments of the string.
- What is a framework in PHP? Name some of them?
A framework provides us a platform for creating PHP web applications with the help of libraries that contain pre-packaged functions and classes to make the development easier. Some of the common frameworks are- CakePHP, Yii 2, Symfony, etc.
In this blog, we enlisted 30 basic PHP Interview questions that are highly probable to be asked in interviews to check your understanding of PHP. In this series of questions, we have covered more questions of intermediate level in PHP Interview questions part-2 and of advanced level in PHP Interview questions part-3.
Do check them out once you have read the above questions. If you like this blog, do share it with your friends!
By: Gorakhnath Yadav