PHP Interview Questions

PHP Interview Questions : PHP is a widely used open source programming language especially suited for creating dynamic websites and mobile API’s. If you are starting your career in php then you must know what types of questions are commonly asked in PHP job interviews. Most of these PHP interview questions are asked to freshers and experienced ones in real interviews in IT industries. Dear readers, these PHP Programming Language Interview questions have been specially designed so that you can get acquainted with the nature of the questions you may be asked during your interviews.

1. What is PHP?

Answer:- PHP(Hypertext Preprocessing) – Used to design and develop dynamic and interactive web pages.
It is a server side scripting language – PHP scripts are run at server.
Interpreter based – Scripts are processed every time it is run. There is no compiled output.


2. What is Client Side Scripting and Server Side Scripting?

Answer:-
Client Side Scripting is :-

1. Scripts executed at client(browsers)
2. Browsers take responsibility of interpreting the scripts
3. Performs client side event handling and validations. Examples: Java Script, Ajax

Server Side Scripting is :-

1. Scripts run on server
2. Generally used to perform secure operations, data handling, database operations, sessions, access and authorization. Examples : Perl, PHP, ASP.


3. What are the features of PHP?

Answer:- The features of PHP are as follows :-

1. Cross platform compatibility: PHP can be compiled and built on all platforms, including most UNIXs, Windows(95/98/NT/2000) and Macs.

2. Database integration: PHP work with nearly all the databases like MySQL, MSSQL, Oracle, Informix, PostgreSQL etc.

3. Object oriented programming: PHP has a support for object oriented concepts.

4. XML Support: PHP 5 has a standardized XML library fully comply with W3 specifications.

5. Error Handling with Exceptions: PHP 5 offers exception handling to check for runtime errors that may lead to application crash.

6. Additional Features: PHP can talk across networks using IMAP, SNMP, NNTP, POP3, or HTTP and PHP5 command-line version allows individual line processing, similar to Perl and awk.


4. What are Variables in PHP?

Answer:- Variables are used to store data, string, numbers, etc..

1. Variables can contain any value – number, string or reference
2. Arrays are special variables to hold multiple values
3. Variables are preceded with $
4. PHP is case sensitive i.e $x is different from $X


5. What is Automatic type conversion in PHP?

Answer:- The type of the value is temporarily auto converted to type required in the current expression is known as Automatic type conversion.
String to numeric conversion results in a number, if the string is starting with a number. If the string is not starting with a number it results in 0.

  
$a="10"; 
echo $a+20;        #prints 30 as $a is assumed as 10


6. What is Get/Set type of variable in PHP?

Answer:- gettype() function returns the type of the variable. settype() function is used to set type to variable. Type may change on next value assignment

  
$a=20;
echo gettype($a);              # integer
settype($a,"string");          # sets type to string
echo gettype($a);              # string


7. What are Constants in PHP?

Answer:- Constants, like variables, can hold integer ,double or string. Cannot be changed after initialization.

  
<?php
define('LIMIT',100);
echo LIMIT;
LIMIT=1000;            # error
?>


8. What are the popular frameworks in PHP?

Answer:- CakePHP, CodeIgniter, Yii 2, Symfony, Zend Framework etc. are the popular frameworks in PHP.


9. What is "echo" in PHP?

Answer:- echo — Output one or more strings. Echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function.


10. What is "print" in PHP?

Answer:- The print() function outputs one or more strings.
Note: The print() is not actually a function, so you are not required to use parentheses with it.
Tip: The print() function is slightly slower than echo().


11. What are magic constants in PHP?

Answer:- PHP magic constants are predefined constants which are globally available to any running PHP script.
Most commonly used magic constants are as follows - __FILE__, __DIR__ , __CLASS__ , __METHOD__


12. What are Cookie in PHP?

Answer:- A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.


13. What is Superglobals in PHP?

Answer:- Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available in all scopes.
Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
Example :- $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET


14. What is Sessions in PHP?

Answer:- A session is a global variable stored on the server. Each session is assigned a unique id which is used to retrieve stored values. Sessions have the capacity to store relatively large data compared to cookies.


15. What are Traits in PHP?

Answer:- Traits are proposed to reduce the gap of single inheritance with reusing of code or methods. The main reason for developing traits like functionality is to reduce the method complexity and avoid the problems related with multiple inheritance and Mixins.


16. What is Class Constants in PHP?

Answer:- Class Constants are constants that are declared within classes. Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword.


17. What does isset() function do?

Answer:- PHP isset() function is used to check if a variable exists in the code or not. It returns the result as Boolean. If we pass any variable in the isset function, it returns the result as either True or False. If the variable that we have passed has been declared and also contain some value other than NULL then it will be returning True as the result otherwise False.


18. What is the array in PHP?

Answer:- An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

  
$myarray=array(10,20,30);


19. What is Access Modifiers in PHP OOPs?

Answer:- Properties and methods can have access modifiers which control where they can be accessed.

Public - Class members with this access modifier will be publicly accessible from anywhere, even from outside the scope of the class.
Private - Class members with this keyword will be accessed within the class itself. It protects members from outside class access with the reference of the class instance.
Protected - Same as private, except by allowing subclasses to access protected superclass members.


20. Why is varible scope in PHP?

Answer:- Variable scope is known as its boundary within which it can be visible or accessed from code. Scope of a variable decides visibility/accessibility and lifetime.
There are only two scopes available in PHP namely local and global scopes.

1. Local variables (local scope)
2. Global variables (special global scope)
3. Static variables (local scope)
4. Function parameters (local scope)


21. What is include() function in PHP?

Answer:- 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 i.e. the script will continue to execute.

 
//lfc.php    
<?php
function add($a,$b)
{
return $a+$b;
}
?>
-------------------------
-------------------------
\\lfc1.php
<?php
include('./lfc.php');
$var1 = 10;
$var2 = 10;
$c = add($var1,$var2);
echo $c;
?>


22. What is require() function in PHP?

Answer:- The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.

 
//lfc.php    
<?php
function add($a,$b)
{
return $a+$b;
}
?>
-------------------------
-------------------------
\\lfc1.php
<?php
echo "Inside the program<br/>";
$var1=10;
$var2=10;
$c=$var1+$var2;
echo $c;
require('./lfc.php');      # can be placed anywhere in PHP code
?>


23. What are the different types of errors in PHP?

Answer:- There are 3 types of error in PHP.

Notices :- These are non-critical errors. These errors are not displayed to the users.
Warnings :- These are more serious errors, but they do not result in script termination. By default, these errors are displayed to the user.
Fatal Errors :- These are the most critical errors. These errors may cause immediate termination of script.


24. What is Regular expressions in PHP?

Answer:- Regular expressions are used to write custom validations. preg_match() performs a regular expression match.

 
$name =“PHP123";
if(preg_match("/^[a-z]+$/", $name ))
echo "valid name";
else
echo "Invalid Name";


25. What is the use of basename() function in PHP?

Answer:- The function basename() can be used to retrieve filename from the path. It takes path and the suffix as arguments.

 
$dpath=“/home/examples/sample.txtâ€;
$filename=basename($dpath,â€.txtâ€);   


26. What is the use of fread() function in PHP?

Answer:- fread() is used to read the entire content of a file and stored into a scalar variable. It trims the newline characters.

Syntax :- string fread(resource handle, int length)
handle :- the filehandle name
length :- specifies the number of bytes to be read


27. What are the differences between PHP4 and PHP5?

Answer:- The difference between PHP4 and PHP5 are as follows :-

PHP4 PHP5
Constructor has same name as the class name Constructor are named as _construct and destructor as _destruct
Everything is passed by value All objects are passed by references.
PHP4 does not declare a class as abstract. PHP5 allows to declare a class as abstract.
It doesn't have static methods and properties in a class. It allow to have static methods and properties in a class.


28. What are the differences between "echo" and "print" in PHP?

Answer:- The differences between "echo" and "print" in PHP are as follows :-

echo print
Not written with parenthesis The can or can not be written with parenthesis
It can output one or more strings It can output only one strings at a time, and returns 1
echo is faster than print print is slower than echo
echo($arg1[, $arg2 .....]) print($arg)


29. What are the differences between GET and POST methods in PHP?

Answer:- The differences between GET and POST method in PHP are as follows :-

GET POST
The GET method is restricted to send upto 1024 characters only The POST method does not have any restrictions on data size to be spent
GET can not be used to send binary data, like images or word documents, to the server. The POST method can be used to send ASCII as well as binary data.
The data sent by GET method can be accessed using QUERY_STRING enviroment variable. The data sent by POST method goes through HTTP header so security depends on HTTP protocol.
The PHP provide $_GET associative array to access all the sent information using GET method. The PHP provide $_POST associative array to access all the sent information using POST method.


30. What are the differences between session and cookie in PHP?

Answer:- The differences between session and cookie in PHP are as follows :-

session cookie
Sessions are stored on server side Cookies are stored on client side
Sessions can store objects cookies can only store strings
When users close their browser they also lost the sessions Cookies can be set to a long lifespan







Also check :


Discussion



* You must be logged in to add comment.