Top 25 PHP Interview Question for Freshers
Top 25 PHP Interview Question And Answers
These are 25 Most Important Question in php that are frequently asked in PHP Interview from freshers . By Preparing these question you can easily crack interview session.
Q1. What is PHP?
PHP is Server Side Scripting Language Commonly used for web applications. It was Developed By Rasmus Lerdorf. PHP stands for Hypertext Preprocessor Language And It is open source server-side scripting language that is widely used for web development. Famous CMS of PHP are WordPress , Joomla And Drupal.
Q2. What is the difference between include and require?
The include() and require() are almost similar.
If the File is not Found by require(), then It will cause a Fatal Error and halt the execution of script but If the File is not Found by include(), then Execution of script will continue and it will issue a warning.
Q3. Difference Between Get and Post Method ?
We can send 1024 bytes using GET method but By using POST method we can transfer large amount of data.
Get Request Remain in the browser History and Post Request do not Remain in the browser History. We don’t use Get Method for sending Sensitive information to server. The POST method can be used to send ASCII as well as binary data.
Q4. What is the use of “echo” in php?
It is used to print a data in the webpage, For Example:
echo “ Hello World” ;
The following code will print the text on the webpage.
Q5. Difference between Session and Cookie?
The main difference between Sessions and cookies is that sessions are stored on the server, and cookies are stored on the user’s computers. Session is mainly used for login and logout purposes.While Cookies are used for activity Tracking.
Session can hold multiple variables But Cookies can not hold multiple variables.Session Remain Active as long browser is open.
Q6. What is cookie And How to set cookies in PHP?
A cookie is a small file that the server embeds or insert on the user’s computer.
To Set Cookie we write this Command.
setcookie(“John”, “Sam”, time()+3600);
Here “John” is cookie name and “Sam” is cookie Value.
Q7. How to Delete cookies in PHP?
To Delete a cookie we set the expiration date to one hour ago.
setcookie(“John”, “Sam”, time() – 3600);
Q8. How to Retrieve a Cookie Value?
To Retrieve Cookie Value we will write this command.
Q9. What types of loops exist in php?
Loops in PHP Are:
for, while, do while and for each loops
Note: Learn its Usage Also.
Q10. Difference between Echo and print Statement?
Echo can accept multiple expressions while print cannot accept multiple expressions. Echo is faster than print since it does not return a value. Print always returns 1 or 0 depending on the success.
Q11. How to create a mysql connection?
The following code can be used to create mysql Connection.
mysql_connect(servername , username , password);
Example: mysql_connect(“localhost”,”Abc”,” “);
Q12. How to redirect a page in php?
The following code can be used to redirect a page:
Q13. What is the significance of “action” attribute in a html form?
The significance of action attribute is to determines where to send the form-data in the form submission.
Example : action=”subscribe.php”
The data will be sent to Page called subscribe.php
Q14. How to define a constant?
It can be defined Using :
define() directive, like define (“CONSTANT”,150)
Q15. How to send email using php?
To send email using PHP, we have to use the mail() function.
$to = “John@abc.com”;
$subject = “Any subject”;
$txt = “Hello How are you”;
$headers = “From: email@example.com” ;
Q16. Difference between explode() and split() functions?
Split function splits string into array by regular expression.
Array (  => H  => e  => y)
Explode Function breaks a string into array.
$str = “Hello world.”;
print_r (explode(” “,$str));
Array (  => Hello  => world.)
Q17 What is the use of isset() in php?
This function is used to check whether a variable is set and is not NULL.
Q18 What is the difference between $var and $$var?
They are both variables.
$var is a variable with a fixed name. $$var is a Reference Variable
$Message = “Hey”;
Explaination : $Message store the variable data and $$Message is a reference variable therefore when you echo $Hey it will give output as “Me”.
Q19. What are the different errors in PHP?
There are three types of runtime errors in Php :
These are some severe errors, Like When we try to include() file which is not available. These errors are showed to the user by default but they will not result script termination.
These errors are small , non-critical errors that we generally come across while executing the script in PHP. Like Trying to access the variable which is not defined.
These are critical errors. These errors results in termination of the script immediately.
Q20. How to delete a file from System in php?
To delete a file from system we use Unlink() function.
Q21. What is the difference between include(), include_once() and require_once() ?
The include() statement will includes and evaluates a specified line. That means it will include a file based in the given path.
require() does the same thing expect upon failure it will generate a fatal error and halt the script whereas include() will just gives a warning and allow script to continue.
require_once() will check if the file already has been included and if so it will not include the file again.
Q22. What is the difference between unset() and unlink() function.
The unset() function is used to destroy a variable where as unlink() function is used to destroy a file.
Q23. What is PDO classes?
The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Using PDO drivers we can connect to database like DB2, Oracle, PostgreSQL etc
Q24. What is the function mysql_pconnect() usefull for?
mysql_pconnect() ensure a persistent connection to the database, it means that the connection do not close when the the PHP script ends.
Q25. What is the difference between session_unregister() and session_unset()?
The session_unregister() function unregister a global variable from the current session and the session_unset() function free all session variables.