PHP Interview Questions & Answers

Note : Check out our Android App for 101 interview Questions

PHP Interview Questions & Answers :

1. What is the difference between echo and print?
echo:

  • Is a command only.
  • Faster than print

print:

  • Is a function.
  • It will return true(1) or false(0) or some values.

2. How can we submit a form without a submit button?
A form in Javascript can be submitted using document.formname.submit();

Other functions like PHP's header("location :$url"); is transfering control from one page to another. It is not submitting form.

In Javascript window.location(url) is also transfering control from one page to another without submission form.

3. what is difference between $message and $$message?

$message in simple variable and $$message in refrence variable.
For example

$message "hello";
$$message $message;

echo $$message;

//It will print hello on the screen

4. What type of inheritance that php supports?.
PHP supports only single inheritance.But multiple inheritance can be implemented in php through use of interface.

5. POST and GET Methods : Which will execute faster POST or GET? Explain?
Once most important difference is when you are sending the form with GET method. It displays name/value pair used in the form at the address bar of the browser preceded by url.

Whereas if you send the form with POST method then user can not see that information and Secondly When you want to send short or small data & Less Sensitive Data then you can use GET Method. But for long data & Sensitive Data sending say more then 100 character you can use POST method

6. Why do we put @ symbol before any variable?
@ symbol when placed before any variable will hide notices and warnings  which are generated when trying to access an undefined variable.


7. What is the difference between session_register and $_session?
Following are differences between session_register and $_SESSION

1. session_register function returns boolean value and $_SESSION returns string value
2. session_register function does'nt work if register_global is disabled. $_SESSION works in both case whether register_global is disabled or enabled. So using $_SESSION for session variable manipulation is more appropriate.

8. what is differenc between mysql_connect and mysql_pconnect?
mysql_connect opens up a database connection every time a page is loaded. mysql_pconnect opens up a connection, and keeps it open across multiple requests. 

mysql_pconnect uses less resources, because it does not need to establish a database connection every time a page is loaded.

9. How do you know (status) whether the recipent of your mail had opened the mail i.e read the mail?
In PHP we can use "Disposition-Notification-To:" in header function.

10. How can you avoid execution time out error while fetching record from mysql?
set_time_limit -- Limits the maximum execution time. It must be increased.

set_time_limit(0);If you set to 0 you say that there is not limit.

11. what are the various methods to pass data from one web page to another web page ?

1.POST 
2.GET 
3.SESSION 
4.COOKIES
5.QUERY STRING

12. What is htaccess? Why do we use this and Where?
.htaccess files are configuration files of Apache Server which provide a way to make configuration changes on a per-directory basis. A file containing one or more configuration directives is placed in a particular document directory and the directives apply to that directory and all subdirectories thereof.

13. What do you need to do to improve the performance (speedy execution) for the script you have written?
There are many things to be considered.If your application based on Database you should think about re-factoring queries try to use high performance queries (Use EXPLAIN to monitor the amount of records retrieved for each query. You can use UNIQUE LIMITWHERE to filter the no of records returned).And also you should be aware of fine tuning configuration for your needs.
In PHP you should use native functions instead of aliases. And also you should choose best function for the job. If you are going to do simple string replace use str_replace than ereg_replace. Use is_int() instead ofis_integer().Use DBG or xdebug to profile your scripts find the bottle neck function and try to re factor if possible.

14. difference between require() and include()?
if filepath not found .. require() teriminates the program and gives fatal error but include() not teriminate the program it gives warning msg and continues to program.

15. How to prevent form hijacking in PHP?
1. Make register_globals to off to prevent Form Injection with malicious data.
2. Make Error_reporting to E_ALL so that all variables will be intialized before using them.
3. Make practice of using htmlentities(), strip_tags(), utf8_decode() and addslashes()  for filtering malicious data in php
4. SQL injection attacks by using mysql_escape_string().
5. User Input Sanitization-Never trust web user submitted data. Follow good clieint side data validation practices with regular expressions before submitting data to the serve.
6. Form Submision Key Validation: A singleton method can be used to generate a Session form key & validating form being submitted for the same value against hidden form key params.

16. Would you initialize your strings with single quotes or double quotes?
Single quote strings are executed faster than double quotes
When we use single quote for string then php will not parse the things between that quote. It simply assign as it is.
But when we use double quotes then it will parse for variables and other things between double quotes.

17. What is the diffrence between Notify URL and Return URL?.
Notify URL: The URL to which PayPal posts information about the transaction via Instant Payment Notification. Must be URL-encoded. Its an optional field have maximum 256 characters length.

Return URL: The URL to which the customer's browser is returned after completing the payment; for example, a URL on your site that displays a "Thank you for your payment" page.

18. If sesio_cache_expire is not set than in how much time will it expire? 
If Session Cache Expire is not set then it will expire in 180 minutes i.e. 3 hrs

If you want to set it then use the following line:
/* set the cache expire to 30 minutes */
session_cache_expire(30);
Now it will expire after 30 minutes.

19. What are cron jobs? Explain in details. ?
CRON is the name of program that enables UNIX users to execute commands or 
scripts (groups of commands) automatically at a specified time/date. It is 
normally used for sys admin commands like makewhatis which builds a search 
database for the man -k command or for running a backup script but can be used 
for anything. A common use for it today is connecting to the internet and 
downloading your email.

20. What’s the difference between accessing a class method via -> and via ::?
 :: is allowed to access methods that can perform static operations, i.e. those, which do not require object initialization.

21. How many ways I can redirect a PHP page? 
Here are the possible ways of php page redirection.

1. Using Java script: 
'; echo 'window.location.href="'.$filename.'";'; echo ''; echo ''; echo ''; echo ''; } } redirect('http://maosjb.com'); ?>

2. Using php function: header("Location:http://maosjb.com "); .