Php Interview Questions

We have covered PHP interview questions in an easy way so that any fresher or experienced developer gets information from these questions. we define each and every topic with an example so that any reader understand easily.

Q1: What is Php?

PHP stands for Hypertext Preprocessor. PHP is a server-side scripting language that is used to develop dynamic websites. Php is an open-source technology so you can free download through the internet.

Q2: Difference between php4 and php5?

php5 is upgrade version of php4 and php5 improved object orientated programming features.

Q3: How to retrieve the data in the result set?

There is four methods to retrieve the data in the result set of MySQL using PHP.
(i) mysql_fetch_row():- this function fetches one row from a result-set and returns it as an enumerated array.

(ii) mysql_fetch_array():- this function Fetch a result row as an associative array, a numeric array, or both.

(iii) mysql_fetch_assoc():- this function Fetch a result row as an associative array.

(iv) mysql_fetch_object():- this function Fetch a result row as an as an object.

Q4: How to submit form without a submit button?

This is very simple because we can use through javascript function.
[php]<script>
document.form.submit();
</script>
[/php]

Q5: What is session?

Ans:- A session is a way to store information (in variables) to be used across multiple pages.
sessions are stored on the server.

Q6: How to create a session?

Ans:- We can create session through session_start() function

Q7: How to set a value in session?

Ans:- We set the value like this $_SESSION[$name]=$value;
Example:- [php]
<?php
$_SESSION[‘username’]=’John’;
?>
[/php]

Q8: How to remove data from session?

Ans:- We can remove data through unset function.
Example:- [php]
<?php
unset($_SESSION[‘username’]);
?>
[/php]

Q9: How to get the value of current session id?

Ans:- session_id() returns the session id for the current session.
Example:- [php]
<?php
session_id();
?>
[/php]

Q10: How to change session id on the page?

Ans:- We can change session id through session_regenerate_id(true) function.
Example:- [php]
<?php
session_regenerate_id(true);
?>
[/php]

Q11: How to destroy all session data?

Ans:- We can destroy all session data through session_destroy(void) or session_unset(void); function.
Example:- [php]
<?php
session_destroy();
or
session_unset();
?>
[/php]

Q12: What is the default session time in php?

Ans:- The default session time in php is until closing of browser.

Q13: What is cookie?

Ans:- Cookies works on client side and it is stored on the user’s browser.

Q14: What is the meaning of a Persistent Cookie?

Ans:- A persistent cookie is permanently stored in a cookie file on the browser’s computer. By default, cookies are temporary and are erased if we close the browser.

Q15: How to set cookies in PHP?

Ans:- We can set cookie through setcookie($cookie_name,$cookie_value,$time) function.
Example:- [php]
<?php
setcookie("username", "John", time()+3600);
?>
[/php]

Q16: How to retrieve cookies value in PHP?

Ans:- We can retrieve cookie through $_COOKIE[$cookie_name].
Example:- [php]
<?php
echo $_COOKIE[‘username’]; //output:-John
?>
[/php]

Q17: How to remove cookie in PHP?

Ans:- We can remove cookie through set the expiration date in the past.
Example:- [php]
<?php
setcookie("username", "", time()-3600);
?>
[/php]

Q18: What is Server?

$_SERVER is an array containing information such as headers, paths, and script locations.

Q19: How to get IP address of client?

Ans:- We can get through
[php]
$_SERVER[‘REMOTE_ADDR’]
[/php]

Q20: How to get previous reference page?

Ans:- We can get through
[php]
$_SERVER[‘HTTP_REFERER’]
[/php]

Q21: How to get browser property?

Ans:- We can get through
[php]
$_SERVER[‘HTTP_USER_AGENT’]
[/php]

Q22: How to get server name?

Ans:- We can get through
[php]
$_SERVER[‘SERVER_NAME’]
[/php]

Q23: Differences between?

1) echo and print
2) include(), include_once(), require(), require_once()
3) print_r() and var_dump()
4) unlink() and unset()
5) GET and POST method
6) Split and Explode
7) array_merge() and array_combine()
8) urlencode and urldecode
9) $variable and $$variable
10) strstr() and stristr()