Php Interview Questions and Answers

Q.1.What is PHP ?
A.PHP: Hypertext Preprocessor is open source server-side scripting language that is widely used for creation of dynamic web applications.It was developed by Rasmus Lerdorf also know as Father of PHP in 1994.PHP is a loosely typed language , we didn’t have to tell PHP which kind of Datatype a Variable is. PHP automatically converts the variable to the correct datatype , depending on its value.
Q.2.What is the difference between == and === operator in PHP ?
A.In PHP == is equal operator and returns TRUE if $a is equal to $b after type juggling and === is Identical operator and return TRUE if $a is equal to $b, and they are of the same data type.
Q.3.What is session in PHP. How to remove data from a session?
A.As HTTP is state protocol.To maintain states on server and share data across multiple pages PHP session are used.PHP sessions are simple way to store data for individual users/client against a unique session ID.Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data,if session id is not present on server PHP creates a new session, and generate a new session ID.
Q.4.What is difference between include,require,include_once and require_once() ?
A.

  • Include :-Include is used to include files more than once in single PHP script.You can include a file as many times you want.
  • Syntax:- include(“file_name.php”);
  • Include Once:-Include once include a file only one time in php script.Second attempt to include is ignored.
  • Syntax:- include_once(“file_name.php”);
  • Require:-Require is also used to include files more than once in single PHP script.Require generates a Fatal error and halts the script execution,if file is not found on specified location or path.You can require a file as many time you want in a single script.
  • Syntax:- require(“file_name.php”);
  • Require Once :-Require once include a file only one time in php script.Second attempt to include is ignored. Require Once also generates a Fatal error and halts the script execution ,if file is not found on specified location or path.
  • Syntax:- require_once(“file_name.php”);
Q.5.How is a constant defined in a PHP script?
A.Defining a Constant in PHPdefine(‘CONSTANT_NAME’,value);
Q.6.What is the difference between unset and unlink ?
A.Unlink: Is used to remove a file from server.
usage:unlink(‘path to file’);
Unset: Is used unset a variable.
usage: unset($var);
Q.7.What is the use of “echo” in php?
A.It is used to print a data in the webpage, For Example: , The following code print the text in the webpage.
Q.8.How to include a file to a php page?
A.We can include a file using “include() ” or “require()” function with file path as its parameter.
Q.9.How to declare an array in php?
A.Eg : var $arr = array(‘apple’, ‘grape’, ‘lemon’);
Q.10.What is use of in_array() function in php ?
A.in_array used to checks if a value exists in an array.
Q.11.What is use of count() function in php ?
A.count() is used to count all elements in an array, or something in an object.
Q.12.What is the difference between Session and Cookie?
A.The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user’s computers in the text file format. Cookies can’t hold multiple variable while session can hold multiple variables..We can set expiry for a cookie,The session only remains active as long as the browser is open.Users do not have access to the data you stored in Session,Since it is stored in the server.Session is mainly used for login/logout purpose while cookies using for user activity tracking.
Q.13.How to set cookies in PHP?
A.Setcookie(“sample”, “ram”, time()+3600);
Q.14.How to Retrieve a Cookie Value?
A. echo $_COOKIE[“user”];
Q.15.How to create a session? How to set a value in session ? How to Remove data from a session?
A.Create session : session_start();
Set value into session : $_SESSION[‘USER_ID’]=1;
Remove data from a session : unset($_SESSION[‘USER_ID’];
Q.16.what types of loops exist in php?
A.for,while,do while and foreach.
Q.17. what is purpose of Json?
A.

  • JSON is a Java Script Object Notation.
  • It is a used for storing and exchanging data.
  • It is a text, written with JSON.
  • JSON is a language independent data format.

JSON exchanging the data between browser and server, and the data can only be text. It is to read the data from a web server and display the data in a web page.

JSON is a text and it can convert any java script object into JSON and JSON to the server. It can easily be sent to and from a server, and used as a data format by any programming language.

JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects:

JSON.parse()

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Q.18. Difference  between isset and unset ?
A. Both of them are variable handling functions and have similar syntax. However, they differ in their functionalities.
While isset() function specifies whether a variable is declared or set in the php script and doesn’t have a null value, an unset() function clears or unset the defined variable in the php script.In simple words, isset() function is used to examine whether the value of a variable exists or not and unset() function is used to destroy a defined variable.
Q.19. what is Normalization ?
A. Normalization is a systematic approach of decomposing tables to eliminate data redundancy(repetition) and undesirable characteristics like Insertion, Update and Deletion Anamolies. It is a multi-step process that puts data into tabular form, removing duplicated data from the relation tables.Normalization is used for mainly two purposes,

  • Eliminating reduntant(useless) data.
  • Ensuring data dependencies make sense i.e data is logically stored.
Q.20. What is canonical meta ?
A. A canonical tag (aka “rel canonical”) is a way of telling search engines that a specific URL represents the master copy of a page. Using the canonical tag prevents problems caused by identical or “duplicate” content appearing on multiple URLs.
Q.21. What are the popular Content Management Systems (CMS) in PHP?
A. 

  • 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).
Q.22. List some of the features of PHP7.
A. 

  • 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
Q.23. What is the difference between “echo” and “print” in PHP?
A. Echo can output one or more string but print can only output one string and always returns 1.
Echo is faster than print because it does not return any value.
Q.24. What is the difference between $message and $$message?
A. $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.
Q.25. What are magic constants in PHP?
A. PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).
Q.26. What is the use of header() function in PHP?
A. 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.
Q.27. How many types of array are there in PHP?
A. There are three types of array in PHP:

  1. Indexed array: an array with a numeric key.
  2. Associative array: an array where each key has its specific value.
  3. Multidimensional array: an array containing one or more arrays within itself.
Q.28. Explain some of the PHP array functions?
A. There are many array functions in PHP:

  • array()
  • array_change_key_case()
  • array_chunk()
  • count()
  • sort()
  • array_reverse()
  • array_search()
  • array_intersect()
Q.29. How to get the length of string?
A. The strlen() function is used to get the length of the string.
Q.30. Explain some of the PHP string functions?
A. There are many array functions in PHP:

  • strtolower()
  • strtoupper()
  • ucfirst()
  • lcfirst()
  • ucwords()
  • strrev()
  • strlen()
Q.31. What are the methods to submit form in PHP?
A. There are two methods GET and POST.
Q.32. How to read a file in PHP?
A. PHP provides various functions to read data from the file. Different functions allow you to read all file data, read data line by line, and read data character by character.PHP file read functions are given below:

  • fread()
  • fgets()
  • fgetc()
Q.33. How to write in a file in PHP?
A. PHP fwrite() and fputs() functions are used to write data into file. To write data into a file, you need to use w, r+, w+, x, x+, c or c+ mode.
Q.34. How to delete file in PHP?
A. The unlink() function is used to delete a file in PHP.
Q.35. How to download file in PHP?
A. The readfile() function is used to download the file in PHP.