Php Interview Questions and Answers
Php Interview Questions and Answers
1.What is PHP ?
Ans.HP: 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.
2.What is the difference between == and === operator in PHP ?
Ans. 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.
3.What is session in PHP. How to remove data from a session?
Ans. 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.
4.What is difference between include,require,include_once and require_once() ?
Ans.
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”);
5.How is a constant defined in a PHP script?
Ans. Defining a Constant in PHPdefine(‘CONSTANT_NAME’,value);
6.What is the difference between unset and unlink ?
Ans. Unlink: Is used to remove a file from server.
usage:unlink(‘path to file’);
Unset: Is used unset a variable.
usage: unset($var);
7.What is the use of “echo” in php?
Ans. It is used to print a data in the webpage, For Example: , The following code print the text in the webpage.
8.How to include a file to a php page?
Ans. We can include a file using “include() ” or “require()” function with file path as its parameter.
9.How to declare an array in php?
Ans. Eg : var $arr = array(‘apple’, ‘grape’, ‘lemon’);
10.What is use of in_array() function in php ?
Ans. in_array used to checks if a value exists in an array.
11.What is use of count() function in php ?
Ans. count() is used to count all elements in an array, or something in an object.
12.What is the difference between Session and Cookie?
Ans. 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.
13.How to set cookies in PHP?
Ans. Setcookie(“sample”, “ram”, time()+3600);
14.How to Retrieve a Cookie Value?
Ans. echo $_COOKIE[“user”];
15.How to create a session? How to set a value in session ? How to Remove data from a session?
Ans. Create session : session_start();
Set value into session : $_SESSION[‘USER_ID’]=1;
Remove data from a session : unset($_SESSION[‘USER_ID’];
16.what types of loops exist in php?
Ans. for,while,do while and foreach.
17. what is purpose of Json?
Ans.
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.
18. Difference between isset and unset ?
Ans. 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.
19. what is Normalization ?
Ans. 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.
20. What is canonical meta ?
Ans. 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.
21. What are the popular Content Management Systems (CMS) in PHP?
Ans.
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.
22. List some of the features of PHP7.
Ans.
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
23. What is the difference between “echo” and “print” in PHP?
Ans. 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.
24. What is the difference between $message and $$message?
Ans. $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.
25. What are magic constants in PHP?
Ans. PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).
26. What is the use of header() function in PHP?
Ans. 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.
27. How many types of array are there in PHP?
Ans. 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.
28. Explain some of the PHP array functions?
Ans. There are many array functions in PHP:
array()
array_change_key_case()
array_chunk()
count()
sort()
array_reverse()
array_search()
array_intersect()
29. How to get the length of string?
Ans. The strlen() function is used to get the length of the string.
30. Explain some of the PHP string functions?
Ans. There are many array functions in PHP:
strtolower()
strtoupper()
ucfirst()
lcfirst()
ucwords()
strrev()
strlen()
31. What are the methods to submit form in PHP?
Ans. There are two methods GET and POST.
32. How to read a file in PHP?
Ans. 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()
33. How to write in a file in PHP?
Ans. 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.
34. How to delete file in PHP?
Ans. The unlink() function is used to delete a file in PHP.
35. How to download file in PHP?
Ans. The readfile() function is used to download the file in PHP.
1.What is PHP ?
Ans.HP: 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.
2.What is the difference between == and === operator in PHP ?
Ans. 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.
3.What is session in PHP. How to remove data from a session?
Ans. 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.
4.What is difference between include,require,include_once and require_once() ?
Ans.
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”);
5.How is a constant defined in a PHP script?
Ans. Defining a Constant in PHPdefine(‘CONSTANT_NAME’,value);
6.What is the difference between unset and unlink ?
Ans. Unlink: Is used to remove a file from server.
usage:unlink(‘path to file’);
Unset: Is used unset a variable.
usage: unset($var);
7.What is the use of “echo” in php?
Ans. It is used to print a data in the webpage, For Example: , The following code print the text in the webpage.
8.How to include a file to a php page?
Ans. We can include a file using “include() ” or “require()” function with file path as its parameter.
9.How to declare an array in php?
Ans. Eg : var $arr = array(‘apple’, ‘grape’, ‘lemon’);
10.What is use of in_array() function in php ?
Ans. in_array used to checks if a value exists in an array.
11.What is use of count() function in php ?
Ans. count() is used to count all elements in an array, or something in an object.
12.What is the difference between Session and Cookie?
Ans. 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.
13.How to set cookies in PHP?
Ans. Setcookie(“sample”, “ram”, time()+3600);
14.How to Retrieve a Cookie Value?
Ans. echo $_COOKIE[“user”];
15.How to create a session? How to set a value in session ? How to Remove data from a session?
Ans. Create session : session_start();
Set value into session : $_SESSION[‘USER_ID’]=1;
Remove data from a session : unset($_SESSION[‘USER_ID’];
16.what types of loops exist in php?
Ans. for,while,do while and foreach.
17. what is purpose of Json?
Ans.
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.
18. Difference between isset and unset ?
Ans. 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.
19. what is Normalization ?
Ans. 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.
20. What is canonical meta ?
Ans. 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.
21. What are the popular Content Management Systems (CMS) in PHP?
Ans.
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.
22. List some of the features of PHP7.
Ans.
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
23. What is the difference between “echo” and “print” in PHP?
Ans. 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.
24. What is the difference between $message and $$message?
Ans. $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.
25. What are magic constants in PHP?
Ans. PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).
26. What is the use of header() function in PHP?
Ans. 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.
27. How many types of array are there in PHP?
Ans. 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.
28. Explain some of the PHP array functions?
Ans. There are many array functions in PHP:
array()
array_change_key_case()
array_chunk()
count()
sort()
array_reverse()
array_search()
array_intersect()
29. How to get the length of string?
Ans. The strlen() function is used to get the length of the string.
30. Explain some of the PHP string functions?
Ans. There are many array functions in PHP:
strtolower()
strtoupper()
ucfirst()
lcfirst()
ucwords()
strrev()
strlen()
31. What are the methods to submit form in PHP?
Ans. There are two methods GET and POST.
32. How to read a file in PHP?
Ans. 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()
33. How to write in a file in PHP?
Ans. 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.
34. How to delete file in PHP?
Ans. The unlink() function is used to delete a file in PHP.
35. How to download file in PHP?
Ans. The readfile() function is used to download the file in PHP.