PHP Interview Questions

1)What is PHP?
PHP: Hypertext Preprocessor:PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.
2)How a constant is defined in a PHP script?
Constants are defined by using the define()directive.PHP constant() is useful if you need to retrieve the value of a constant. PHP Constant value cannot change during the execution of the code. By default constant is case-sensitive
<html>
<head>
<title>My First php Constant Program</title>
</head>
<body>
<?php
define(“numbers”, 1234);
define(“TEXT”, “php”);
echo constant (“numbers”);
echo constant (“TEXT”);
?>
</body>
</html>
output: 1234php
3)What are the Difference Between strstr() and stristr()?
This strstr() function is used to return the character from a string from specified position
Example:
<html>
<head>
<title>My First String strstr Program</title>
</head>
<body>
<?php
$str=”welcome to php”;
echo strstr($str,’t’);
?>
</body>
</html>
Output: to php
stristr()function
This function is same as strstr is used to return the character from a string from specified position,But it is CASE INSENSITIVE
Example:
<html>
<head>
<title>My First String strIstr Program</title>
</head>
<body>
<?php
$str=”WELCOME TO PHP”;
echo stristr($str,’t’);
?>
</body>
</html>
output:TO PHP
4)Differences between Get and post methods in PHP?.
GET and POST methods are used to send data to the server, both are used to same purpose .But both are some defferences
GET Method have send limit data like only 2Kb data able to send for request and data show in url,it is not safety.
POST method unlimited data can we send for request and data not show in url,so POST method is good for send sensetive request
5)How do I find out the number of parameters passed into function. ?
func_num_args() function returns the number of parameters passed in.
6)What is 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.
urlencode Example program:
<?php
$arr=urlencode(“20.00%”);
echo $arr;
?>
Here urlencode(“20.00%”) will return “20.00%25. URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.
urldecode Example Program:
<?php
$arr=urldecode(“20.00%”);
echo $arr;
?>
output:20.00%
7)How to increase the execution time of a php script?
To Change max_execution_time variable in php.ini file .By Default time is 30 seconds
The file path is xampp/php/php.ini
8)Difference between htmlentities() and htmlspecialchars()?
htmlentities() – Convert ALL special characters to HTML entities
htmlspecialchars() – Convert some special characters to HTML entities (Only the most widely used)
9)What is the difference between $message and $$message?
$message is a simple variable whereas $$message is a reference variable.
Example:
<?php
$message = “php”;
$php= “cms”;
echo $message;
echo ‘<br>’;
echo $$message;
?>
output:
php
cms
10)How To Get the Uploaded File Information in the Receiving Script?
Once the Web server received the uploaded file, it will call the PHP script specified in the form action attribute to process them. This receiving PHP script can get the uploaded file information through the predefined array called $_FILES.
$_FILES[$fieldName]['name'] – The Original file name on the browser system.
$_FILES[$fieldName]['type'] – The file type determined by the browser.
$_FILES[$fieldName]['size'] – The Number of bytes of the file content.
$_FILES[$fieldName]['error'] – The error code associated with this file upload.
$_FILES[$fieldName]['tmp_name'] – The temporary filename of the file in which the uploaded file was stored on the server.
11)What is difference between Array combine() and Array merge()?
Array_combine(): To Combines the elements of two arrays and Returns the New Array.The new Array keys are the values of First Array values and the new array values are Second array values.
Array_combine()Example:
<?php
$arr=array(10,30,50);
$arr1=array(45,67,54);
print_r(array_combine($arr,$arr1));
?>
Output:
Array
(
[10] => 45
[30] => 67
[50] => 54
)
Array_merge(): merges two or more Arrays as New Array.
Array_merge() Example:
<?php
$arr=array(10,30,50);
$arr1=array(45,67,54);
print_r(array_merge($arr,$arr1));
?>
output:
Array
(
[0] => 10
[1] => 30
[2] => 50
[3] => 45
[4] => 67
[5] => 54
)
12)What is Implode() Function in PHP?
The implode function is used to “join elements of an array with a string”.
Example:
<?php
$arr=array (“open”,”source”,”cms”);
echo implode(” “,$arr);
?>
Output: open source cms
13)what Explode() Function in PHP?
The explode function is used to “Split a string by a specified string into pieces i.e. it breaks a string into an array”.
Example:
<?php
$str = “Gampa venkateswararao vaidana ballikurava “;
print_r (explode(” “,$str));
?>
output:
Array
(
[0] => Gampa
[1] => venkateswararao
[2] => vaidana
[3] => ballikurava
[4] =>
)
14)What does a special set of tags do in PHP?
in PHP special tags <?= and ?> the use of this Tags are The output is displayed directly to the browser.
15)What is the difference between md5(),crc32() and sha1() in PHP?
All of these functions generate hashcode from passed string argument.
The major difference is the length of the hash generated
crc32() gives 32 bit code
sha1() gives 128 bit code
md5() gives 160 bit code
16)What are the differences between require() and include()?
Both include() and require() used to include a file ,if the code from a file has already have included,it will not be included again. we have to use this code multiple times .But both are some minor Difference Include() send a Warning message and Script execution will continue ,But Require() send Fatal Error and to stop the execution of the script.
17)What are the differences between require_once(), 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.
18)What is session in php?
session variable is used to store the information about a user and the information is available in all the pages of one application and it is stored on the server.
An user sends the first Request to the Server for every new user webserver creates an Unique ID The Request is called as Session_id.Session_id is an unique value generated by the webserver. To start a session by using session_start()
Starting a PHP Session
Example:
<?php
// this starts the session
session_start();
// this sets variables in the session
$_SESSION['Name']=’Venki’;
$_SESSION['Qulification']=’MCA’;
$_SESSION['Age']=27;
?>
Add or access session variables by using the $_SESSION superglobal array.
How to Retrive Data?
<?php
session_start();
echo “Name = ” $_SESSION["Name"];// retrieve session data
?>
To delete a single session value, you use the unset() function:
<?php
session_start();
unset($_SESSION["Name"]);
?>
How to Destroy the Session?
<?php
session_start();
session_destroy();// terminate the session
?>
19)What is the default session time in php?
The default session time in php is until closing of browser
20)What is Cookie and How to Create a Cookie in php?
Cookie is a piece of information that are stored in the user Browser memory, usually in a temporary folder. Cookies can be useful as they store persistent data you can access from different pages of your Website, but at the same data they are not as secure as data saved in a server
in PHP, you can both create and retrieve cookie values.
How to Create a Cookie?
using The setcookie() function is used to set a cookie.
Syntax
setcookie(name, value, expire, path, domain);
Example
<?php
setcookie(“user”, “venki”, time()+3600);
?>
How to a retrieve a cookie value?
$_COOKIE variable is used to retrieve a cookie value.
Example:
<?php
echo $_COOKIE["user"];// Print a cookie
print_r($_COOKIE); // view all cookies
?>
How to Accessing a cookie?
In the following example we use the isset() function to find out if a cookie has been set:
<?php
if( isset($_COOKIE['username']) ) //storing the value of the username cookie into a variable
{
$username = $_COOKIE['username'];
}
?>
21)How can you destroy or Delete a Cookie?
Destroy a cookie by specifying expire time in the past:
Example: setcookie(’opensource’,’php’,time()-3600); // already expired time
22)Difference between Persistent Cookie and Temporary 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:
· Temporary cookies can not be used for tracking long-term information.
· Persistent cookies can be used for tracking long-term information.
· Temporary cookies are safer because no programs other than the browser can
access them.
· Persistent cookies are less secure because users can open cookie files see the
cookie values
23)Difference Between Cookies and Sessions in php?
Cookies and sessions both are used to store values or data,But there are some differences a cookie stores the data in your browser memory and a session is stored on the server.
Cookie data is available in your browser up to expiration date and session data available for the browser run, after closing the browser we will lose the session information.
Cookies can only store string data,but session stored any type of data.we could be save cookie for future reference, but session couldn’t. When users close their browser, they also lost the session.
Cookies are unsecured,but sessions are highly Secured.
You lose all the session data once you close your browser, as every time you re-open your browser, a new session starts for any website.
Cookies stores some information like the username, last visited Web pages etc. So that when the customer visits the same site again, he may have the same environment set for him. You can store almost anything in a browser cookie.when you check the ‘Remember Password’ link on any website, a cookie is set in your browser memory, which exists there in the browser until manually deleted. So, when you visit the same website again, you don’t have to re-login.
The trouble is that a user can block cookies or delete them at any time. If, for example, your website’s shopping cart utilized cookies, and a person had their browser set to block them, then they could not shop at your website.
24)How to connet mysql database with PHP ?
$con = mysql_connect(“localhost”, “username”, “password”);
Example:
<?php
if($con=mysql_connect(“localhost”,”root”,””))
echo “connected”;
else
echo “not connected”;
if(mysql_query(“create Database emp”,$con))
echo “Database Created”;
else
echo “Database not Created”;
?>
output:
connectedDatabase Created
25)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.
26)Different Types of Tables in Mysql?
There are Five Types Tables in Mysql
1)INNODB
2)MYISAM
3)MERGE
4)HEAP
5)ISAM
27)What is the Difference between INNODB and MYISAM?
The main difference between MyISAM and InnoDB is that InnoDB supports transaction
InnoDB supports some newer features: Transactions, row-level locking, foreign keys
MYISAM:
1. MYISAM supports Table-level Locking
2. MyISAM designed for need of speed
3. MyISAM does not support foreign keys hence we call MySQL with MYISAM is DBMS
4. MyISAM stores its tables, data and indexes in diskspace using separate three different files. (tablename.FRM, tablename.MYD, tablename.MYI)
5. MYISAM not supports transaction. You cannot commit and rollback with MYISAM. Once you issue a command it’s done.
INNODB:
1. InnoDB supports Row-level Locking
2. InnoDB designed for maximum performance when processing high volume of data
3. InnoDB support foreign keys hence we call MySQL with InnoDB is RDBMS
4. InnoDB stores its tables and indexes in a tablespace
5. InnoDB supports transaction. You can commit and rollback with InnoDB
28) What is the difference between mysql_fetch_object() and mysql_fetch_array()?
The mysql_fetch_object() function collects the first single matching record
mysql_fetch_array() collects all matching records from the table in an array.
28)How many ways we can retrieve the date in result set of mysql using php?
The result set can be handled using 4 ways
1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc
29)What are the differences between DROP a table and TRUNCATE a table?
DROP TABLE table_name – This will delete the table and its data.
TRUNCATE TABLE table_name – This will delete the data of the table, but not the table definition.
30)What is the maximum length of a table name, a database name, or a field name in MySQL?
Database name: 64 characters
Table name: 64 characters
Column name: 64 characters
31)What is the difference between CHAR and VARCHAR data types?
CHAR is a fixed length data type. CHAR(n) will take n characters of storage even if you enter less than n characters to that column.
VARCHAR is a variable length data type. VARCHAR(n) will take only the required storage for the actual number of characters entered to that column.
32)How can we know that a session is started or not?
A session starts by using session_start() function.