20+ Popular interview questions and answers for php freshers

PHP is very popular open source scripting languages, so there are a lot of jobs for freshers.I have cater best common interview question for php beginner and php fresher job.These all question are belong to PHP language so it help to crack PHP round u interview.You can get MySQL interview questions with answer from my previous article Important MySQL questions and Answers

I have listed down all popular PHP interview questions with detail answer.You can read and found your dream job.Best Of luck for your interview.

How can we submit a form without a submit button?

If you don’t want to use the Submit button to submit a form, you can use normal hyper links to submit a form. But you need to use some JavaScript code in the URL of the link. For example:

Select Code
<a href="javascript: document.myform.submit();">Submit Me</a>

What is the difference between $message and $$message?

Anwser 1:
$message is a simple variable whereas $$message is a reference variable. Example:
$user = 'bob'

is equivalent to

Select Code
$holder = 'user';
$$holder = 'bob';

They are both variables. But $message is a variable with a fixed name. $$message is a variable who’s name is stored in $message. For example, if $message contains "var", $$message is the same as $var.

What Is a Persistent Cookie?

A persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer. By default, cookies are created as temporary cookies which stored only in the browser’s memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:

  1. Temporary cookies can not be used for tracking long-term information.
  2. Persistent cookies can be used for tracking long-term information.
  3. Temporary cookies are safer because no programs other than the browser can access them.
  4. Persistent cookies are less secure because users can open cookie files see the cookie values.

How do you define a constant?

Select Code
Via define() directive, like define ("MYCONSTANT", 100);

What are the differences between require and include, include_once?

require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again.

But require() and include() will do it as many times they are asked to do.

What is meant by urlencode() and urldecode()?

urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode("10.00%") will return "10%2E00%25″. URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.

For example:

Select Code
$discount ="10.00%";
$url = "http://domain.com/submit.php?disc=".urlencode($discount);
echo $url;

You will get “http://domain.com/submit.php?disc=10%2E00%25″.

How can I execute a PHP script using command line?

Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.
Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.

What is the difference between mysql_fetch_object and mysql_fetch_array?

mysql_fetch_object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array

What is the functionality of the functions STRSTR() and STRISTR()?

string strstr ( string haystack, string needle ) returns part of haystack string from the first occurrence of needle to the end of haystack. This function is case-sensitive.

Select Code
stristr() is idential to strstr() except that it is case insensitive.

When are you supposed to use endif to end the conditional statement?

When the original if was followed by : and then the code block without braces.

How can we send mail using JavaScript?

No. There is no way to send emails directly using JavaScript.

But you can use JavaScript to execute a client side email program send the email using the "mailto" code. Here is an example:

Select Code
function myfunction(form)
return true;

What is the functionality of the function strstr() and stristr()?

strstr() returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".
stristr() is identical to strstr() except that it is case insensitive.

What is the difference between ereg_replace() and eregi_replace()?

eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.

How do I find out the number of parameters passed into function(). ?

func_num_args() function returns the number of parameters passed in.

What Is a Session??

A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.

There is only one session object available to your PHP scripts at any time.

Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.

How To Protect Special Characters in Query String?

If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode().

What are the different types of errors in PHP?

Here are three basic types of runtime errors in PHP:

1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although you can change this default behavior.

2. Warnings: These are more serious errors – for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

3. Fatal errors: These are critical errors – for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.

What’s the special meaning of __sleep and __wakeup?

__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.