Interview Questions PHP

PHP


1. What is PHP?
PHP stand for Hypertext Preprocessor. PHP is a Server Side Scripting Language. PHP is an Open Source Software. PHP is free to download and use. PHP scripts are executed on server. PHP supports many databases such as MYSQL, Informix, Oracle, Sybase, Solid,PostgreSQL, Generic ODBC, etc.

2. What is the method available in form submitting?
GET and POST.

3. What are the differences between GET and POST methods in form submitting?
On the server side: 
the main difference between GET and POST is where the submitted is stored. The $_GET array stores data submitted by the GET method. The $_POST array stores data submitted by the POST method.

4. 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. Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.
Code to define session in php
$_SESSION['variable_name']=value;

5. What's the difference between a cookie and session in PHP?
PHP sessions improve upon cookies because they allow web applications to store and retrieve more information than cookies.

Key difference:
Sessions store data on the server, not on the browser like cookies.
Sessions are more secure than cookies.
PHP sessions, unlike cookies which are just stored on the user's browser, need a temporary directory on the server where PHP can store the session data.
Sessions must use the session_start() function

6. How many ways we can pass the variable through the navigation between the pages?
Register the variable into the session 
Pass the variable as a cookie
Pass the variable as part of the URL

7. What are the different functions in sorting an array?
asort()
arsort()
ksort()
krsort()
uksort()
sort()
rsort()

8. How can we know the total number of elements of Array?
sizeof($array_variable)
count($array_variable)
Note: If we just pass a simple variable instead of a an array it will return 1.

9. Define PHP header() Function
The header() function sends a raw HTTP header to a client.
It is important to notice that header() must be called before any actual output is sent.

Example:
Prevent page caching:
<?php
// Date in the past
header("Expires: Mon, 26 Jul 1987 07:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>

10. How can we extract string 'abc.com' from a string 'http://info@abc.com' using regular _expression of php?
We can use the preg_match() function with "/.*@(.*)$/" as the regular expression pattern.

Example:
<?phppreg_match("/.*@(.*)$/","http://info@domain.com",$data); echo $data[1]; ?>