PHP INTERVIEW QUESTIONS AND ANSWERS 2021
Q1 What is PHP?
Answer : PHP is a web language based on scripts that allow developers to dynamically create generated web pages.
Q2 What do the initials of PHP stand for?
Answer : PHP means PHP: Hypertext Preprocessor.
Q3 Which programming language does PHP resemble?
Answer : PHP syntax resembles Perl and C
Q4 What does PEAR stand for?
Answer : PEAR means “PHP Extension and Application Repository”. It extends PHP and provides a higher level of programming for web developers.
Q5 What is the actually used PHP version?
Answer : Version 7.1 or 7.2 is the recommended version of PHP.
Q6 How do you execute a PHP script from the command line?
Answer : Just use the PHP command line interface (CLI) and specify the file name of the script to be executed as follows:
php script.php
Q7 How to run the interactive PHP shell from the command line interface?
Answer : Just use the PHP CLI program with the option -a as follows:
Q8 What is the correct and the most two common way to start and finish a PHP block of code?
Answer : The two most common ways to start and finish a PHP script are:
<?php [ --- PHP code---- ] ?> and <? [--- PHP code ---] ?>
Q9 How can we display the output directly to the browser?
Answer : To be able to display the output directly to the browser, we have to use the special tags <?= and ?>.
Q10 What is the main difference between PHP 4 and PHP 5?
Answer : PHP 5 presents many additional OOP (Object Oriented Programming) features.
Q 11 How can we check the value of a given variable is a number?
Answer : It is possible to use the dedicated function, is_numeric() to check whether it is a number or not.
Q12 How can we check the value of a given variable is alphanumeric?
Answer : It is possible to use the dedicated function, ctype_alnum to check whether it is an alphanumeric value or not.
Q13 How do I check if a given variable is empty?
Answer : If we want to check whether a variable has a value or not, it is possible to use the empty() function.
Q14 What does the unlink() function mean?
Answer : The unlink() function is dedicated for file system handling. It simply deletes the file given as entry.
Q15 What does the unset() function mean?
Answer : The unset() function is dedicated for variable management. It will make a variable undefined.
Q16 How do I escape data before storing it in the database?
Answer : The addslashes function enables us to escape data before storage into the database.
Q17 How is it possible to remove escape characters from a string?
Answer : The stripslashes function enables us to remove the escape characters before apostrophes in a string.
Q18 How can we automatically escape incoming data?
Answer : We have to enable the Magic quotes entry in the configuration file of PHP.
Q19 What is the difference between session and cookie?
Answer : The main difference between session and cookie is that cookies are stored on 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, Session can hold multiple variables. You can manually set an expiry for a cookie, while session only remains active as long as browser is open.
Q20 What does the function get_magic_quotes_gpc() means?
Answer : The function get_magic_quotes_gpc() tells us whether the magic quotes is switched on or no.
Q21 Is it possible to remove the HTML tags from data?
Answer : The strip_tags() function enables us to clean a string from the HTML tags.
Q22 What is PEAR in PHP?
Answer : 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.
Q23 Who is known as the father of PHP?
Answer : Rasmus Lerdorf
Q24 What was the old name of PHP?
Answer : The old name of PHP was Personal Home Page.
Q25 Explain the difference b/w static and dynamic websites?
Answer : In static websites, content can’t be changed after running the script. You can’t change anything on the site. It is predefined.
In dynamic websites, content of script can be changed at the run time. Its content is regenerated every time a user visit or reload. Google, yahoo and every search engine is the example of dynamic website.
Q26 What is the name of scripting engine in PHP?
Answer : The scripting engine that powers PHP is called Zend Engine 2.
Q27 Explain the difference between PHP4 and PHP5.
Answer : PHP4 doesn’t support oops concept and uses Zend Engine 1.
PHP5 supports oops concept and uses Zend Engine 2.
Q28 What are the popular Content Management Systems (CMS) in PHP?
Answer :
- 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).
Q29What are the popular frameworks in PHP?
Answer :
- CakePHP
- CodeIgniter
- Yii 2
- Symfony
- Zend Framework etc.
Q30 Which programming language does PHP resemble to?
Answer : PHP has borrowed its syntax from Perl and C.
Q31 List some of the features of PHP7.
Answer :
- 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
Q32 What is “echo” in PHP?
Answer : PHP echo output one or more string. It is a language construct not a function. So the use of parentheses is not required. But if you want to pass more than one parameter to echo, the use of parentheses is required.
Q33 What is the difference between $message and $$message?
Answer : $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.
Q34 What are the ways to define a constant in PHP?
Answer : PHP constants are name or identifier that can’t be changed during execution of the script. PHP constants are defined in two ways:
- Using define() function
- Using const() function
Q35 What are magic constants in PHP?
Answer : PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).
Q35 How many data types are there in PHP?
Answer : PHP data types are used to hold different types of data or values. There are 8 primitive data types which are further categorized in 3 types:
- Scalar types
- Compound types
- Special types
Q36 How to do single and multi line comment in PHP?
Answer : PHP single line comment is made in two ways:
- Using // (C++ style single line comment)
- Using # (Unix Shell style single line comment)
PHP multi-line comment is made by enclosing all lines within.
Q37 What are the different loops in PHP?
Answer : For, while, do-while and for each.
Q38 What is the use of count() function in PHP?
Answer : The PHP count() function is used to count total elements in the array, or something an object.
Q39 What is the use of header() function in PHP?
Answer : 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.
Q40 What does isset() function?
Answer : The isset() function checks if the variable is defined and not null.
Q41 Explain PHP parameterized functions.
Answer : PHP parameterized functions are functions with parameters. You can pass any number of parameters inside a function. These given parameters act as variables inside your function. They are specified inside the parentheses, after the function name. Output depends upon dynamic values passed as parameters into the function.
Q42 Explain PHP variable length argument function
Answer : PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do this, you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable length argument since PHP 5.6.
Q43 Explain PHP variable length argument function.
Answer : PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments.
Q44 What is the array in PHP?
Answer : 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.
Q45 How many types of array are there in PHP?
Answer : There are three types of array in PHP:
- Indexed array: an array with a numeric key.
- Associative array: an array where each key has its specific value.
- Multidimensional array: an array containing one or more arrays within itself.
Q46 Explain some of the PHP array functions?
Answer : There are many array functions in PHP:
- array()
- array_change_key_case()
- array_chunk()
- count()
- sort()
- array_reverse()
- array_search()
- array_intersect()
Q47 What is a session?
Answer : PHP Engine creates a logical object to preserve data across subsequent HTTP requests, which is known as session. Sessions generally store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same user. Simply, it maintains data of an user (browser).
Q48 What is the method to register a variable into a session?
<?php Session_register($ur_session_var); ?>
Q49 What is $_SESSION in PHP?
A session creates a file in a temporary directory on the server where registered session variables and their session id are stored. This data will be available to all pages on the site amid that visit.
The area of the temporary record is controlled by a setting in the php.ini document called session.save_path.
At the point when a session is begun following things happen –
PHP first makes two duplicates of one of a kind session id for that particular session of the client which is an arbitrary string of 32 hexadecimal numbers, for example, 3c7foj34c3jjhkyepop2fc937e3443.
One copy of unique session id automatically sent to the user?s computer for the sake of synchronization in future ahead, and one copy is being maintained at server side till the session is running.
Whenever you want to access the page of website or web app, then session id of the current user will be associated with the HTTP header, and that will be compared by the session id which is being maintained at the server. After completing the comparison process, you can easily access the page of the website or web app
A session ends when the user closes the browser, or after leaving the site, the server will terminate the session after a predetermined period, commonly 30 minutes duration.
Q50 What is PHP session_start() and session_destroy() function?
PHP session_start() function is used to start the session. It starts new or resumes the current session. It returns the current session if the session is created already. If the session is not available, it creates and returns new sessions.