PHP Interview Questions
PHP Interview Questions
Q:1 What are the differences between Get and post methods in form submitting.
give the case where we can use get and we can use post methods?
A:1 When to use GET or POST
The include_once()
statement includes and evaluates the
specified file during the execution of
the script. This is a behavior similar
to the include()
statement, with the only difference
being that if the code from a file has
already been included, it will not be
included again. As the name suggests, it
will be included just once.include_once()
should be used in cases where the same
file might be included and evaluated
more than once during a particular
execution of a script, and you want to
be sure that it is included exactly once
to avoid problems with function
redefinitions, variable value
reassignments, etc.
require_once()
should be used in cases where the same
file might be included and evaluated
more than once during a particular
execution of a script, and you want to
be sure that it is included exactly once
to avoid problems with function
redefinitions, variable value
reassignments, etc.
Q:1
Can we use include (”abc.PHP”) two times in a PHP page “makeit.PHP”?
0
A:10 Yes we can use include() more than one time in any page though it is
not a very good practice.
Q:1 What are the different tables present in MySQL, which type of
1 table is generated when we are creating a table in the following syntax:
create table employee (eno int(2),ename varchar(10)) ?
A:11 Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23 and as a result if
we do not specify the table name explicitly it will be assigned to the
default engine.
Q:1
Functions in IMAP, POP3 AND LDAP?
2
A:12 You can find these specific information in PHP Manual.
Q:1
How can I execute a PHP script using command line?
3
A:13 As of version 4.3.0, PHP supports a new SAPI type (Server
Application Programming Interface) named CLI which means Command Line
Interface. 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.
Q:1 Suppose your Zend engine supports the mode <? ?> Then how can u
4 configure your PHP Zend engine to support <?PHP ?> mode ?
A:14 In php.ini file:
set
short_open_tag=on
to make PHP support
Q:1 Shopping cart online validation i.e. how can we configure Paypal,
5 etc.?
A:15 We can find the detail documentation about different paypal
integration process at the following site
PayPal PHP
SDK : http://www.paypaldev.org
Q:1
What is meant by nl2br()?
6
A:16 Inserts HTML line breaks (<BR />) before all newlines in a string
string nl2br (string); Returns string with ” inserted before all
newlines. For example: echo nl2br(“god bless\n you”) will output “god
bless <br /> you” to your browser.
Q:1
Draw the architecture of Zend engine?
7
A:17 The Zend Engine is the internal compiler and runtime engine used by
PHP4. Developed by Zeev Suraski and Andi Gutmans, the Zend Engine is an
abbreviation of their names. In the early days of PHP4, it worked as
follows:
The PHP script was loaded by the
Zend Engine and compiled into Zend
opcode. Opcodes, short for operation codes, are low level binary
instructions. Then the opcode was executed and the HTML generated sent
to the client. The opcode was flushed from memory after execution.Today, there are a
multitude of products and techniques to help you
speed up this process. In the following diagram, we show the how modern
PHP scripts work; all the shaded boxes are optional.
http://dev.mysql.com/downloads/mysql/,
If needle is not a
string, it is converted to an integer and applied as the
ordinal value of a character.
Q:4
How many ways can we get the value of current session id?
2
A:42 session_id() returns the session id for the current session.
Q:4 How can we destroy the session, how can we unset the variable of
3 a session?
A:43 session_unregister — Unregister a global variable from the current
session
session_unset — Free all session variables
Q:4
How can we destroy the cookie?
4
A:44 Set the cookie in past.
Q:4 How many ways we can pass the variable through the navigation
5 between the pages?
A:45 GET/QueryString
POST
Q:4
What is the difference between ereg_replace() and eregi_replace()?
6
A:46 eregi_replace() function is identical to ereg_replace() except that
this ignores case distinction when matching alphabetic
characters.eregi_replace() function is identical to ereg_replace()
except that this ignores case distinction when matching alphabetic
characters.
Q:4
What are the different functions in sorting an array?
7
A:47 Sort(), arsort(),
asort(), ksort(),
natsort(), natcasesort(),
rsort(), usort(),
array_multisort(), and
uksort().
Q:4
How can we know the count/number of elements of an array?
8
A:48 2 ways
a) sizeof($urarray) This function is an alias of count()
b) count($urarray)
Q:4 What is the PHP predefined variable that tells the What types of
9 images that PHP supports?
A:49 Though i am not sure if this is wrong or not, With the exif
extension you are able to work with image meta data.
Q:5 How can I know that a variable is a number or not using a
0 JavaScript?
A:50 bool is_numeric ( mixed var)
Returns TRUE if var is a number or a numeric string, FALSE otherwise.or use
isNaN(mixed var)The isNaN() function is used to check if a value is not a number.
Q:5 List out some tools through which we can draw E-R diagrams for
1 mysql.
A:51 Case Studio
Smart Draw
Q:5 How can I retrieve values from one database server and store them
2 in other database server using PHP?
A:52 we can always fetch from one database and rewrite to another. here
is a nice solution of it.$db1 = mysql_connect(“host”,”user”,”pwd”)
mysql_select_db(“db1″, $db1);
$res1 = mysql_query(“query”,$db1);$db2 = mysql_connect(“host”,”user”,”pwd”)
mysql_select_db(“db2″, $db2);
$res2 = mysql_query(“query”,$db2);At this point you can only fetch records from you
previous ResultSet,
i.e $res1 – But you cannot execute new query in $db1, even if you
supply the link as because the link was overwritten by the new db.so at this point the
following script will fail
$res3 = mysql_query(“query”,$db1); //this will failSo how to solve that?
Classes can be created for use on multiple tag types in the document.
Selector and grouping methods can be used to apply styles under complex
contexts. No additional downloads necessary to receive style information
Disadvantages
This method can not control styles for multiple documents at once
Inline Styles
Advantages
Disadvantages
Unique Key: Unique Keys are used to uniquely identify each row in the
table. There can be one and only one row for each unique key value. So
NULL can be a unique key.There can be only one primary key for a table but there can be
more
than one unique for a table.
The structure of table view buyers is as follows:
Q:7
3
Field Type Null Key Default Extra
user_pri_id int(15) PRI null auto_increment
userid varchar(10) YES null
the value of user_pri_id the last row 999 then What will happen in
the following conditions?
Condition1: Delete all the rows and insert another row then.
What is the starting value for this auto incremented field user_pri_id ,
Condition2: Delete the last row(having the field value 999) and
insert another row then. What is the value for this auto incremented
field user_pri_id
A:73 In both cases let the value for auto increment field be n then next
row will have value n+1 i.e. 1000
Q:7
What are the advantages/disadvantages of MySQL and PHP?
4
A:74 Both of them are open source software (so free of cost), support
cross platform. php is faster then ASP and JSP.
Q:7
What is the difference between GROUP BY and ORDER BY in Sql?
5
A:75 ORDER BY [col1],[col2],…,[coln]; Tels DBMS according to what columns
it should sort the result. If two rows will hawe the same value in col1
it will try to sort them according to col2 and so on.GROUP BY
[col1],[col2],…,[coln]; Tels DBMS to group results with same value of
column col1. You can use COUNT(col1), SUM(col1), AVG(col1) with it, if
you want to count all items in group, sum all values or view average
Q:7
What is the difference between char and varchar data types?
6
A:76 Set char to occupy n bytes and it will take n bytes even if u r
storing a value of n-m bytes
Set varchar to occupy n bytes and it will take only the required space
and will not use the n bytes
eg. name char(15) will waste 10 bytes if we store ‘mizan’, if each char
takes a byte
eg. name varchar(15) will just use 5 bytes if we store ‘mizan’, if each
char takes a byte. rest 10 bytes will be free.
Q:7
What is the functionality of md5 function in PHP?
7
A:77 Calculate the md5 hash of a string. The hash is a 32-character
hexadecimal number. I use it to generate keys which I use to identify
users etc. If I add random no techniques to it the md5 generated now
will be totally different for the same string I am using.
Q:7
How can I load data from a text file into a table?
8
A:78 you can use LOAD DATA INFILE file_name; syntax to load data
from a text file. but you have to make sure thata) data is delimited
b) columns and data matched correctly
Q:7 How can we know the number of days between two given dates using
9 MySQL?
A:79 SELECT DATEDIFF(’2007-03-07′,’2005-01-01′);
Q:8 How can we know the number of days between two given dates using
0 PHP?
A:80 $date1 = date(‘Y-m-d’);
$date2 = ’2006-08-15′;
$days = (strtotime($date1) – strtotime($date2)) / (60 * 60 * 24);
PHP-MySQL Interview Questions – 2
Q:8
What is ‘float’ property in CSS?
1
A:8 The float property sets where an image or a text will appear in another
1 element.
Q:8
What is descendant structure in CSS?
2
A:8 Descendant selectors are used to select elements that are descendants
2 of another element in the document tree.For example, you may wish
to target a specific <em> element on the page, but not all <em>
elements. A sample document could contain the following code:
<body>
<h1>Heading <em>here</em> </h1>
<p>Lorem ipsum dolor <em>sit</em> amet.</p>
</body> The document tree diagram (with the <em> element to be
p em {color: blue; } You can also jump levels in the document tree
structure to select descendants. For example, the following code:
<body>
<p>Lorem ipsum dolor <em>sit</em> amet.</p>
<ul>
<li>item 1</li>
<li>item 2</li>
<li><em>item 3</em></li>
</ul>
</body> The document tree (with a third-level <em> element
highlighted) would be:
Using the following rule you can isolate any <em> element inside a
<ul> element, without having to describe the <li> element. If this rule
is applied, any <em> element within a <ul> element will be colored
blue. However, the <em> element within the <p> will not be colored
blue:
For example, you may wish to target an <em> that is a direct child of
a <div>, but not other <em> elements that are descendants of the
<div>. A sample document could contain the following code:
<body>
<h1>Heading <em>text</em></h1>
<div>
This is some <em>text</em>
<p>This is a paragraph of <em>text</em></p>
</div>
</body>
The document tree (highlighting the <em> that is a child of the
<div>) would be:
Using the following rule you can target any <em> element that is a
child of the <div>. Other <em> elements that are descendants but
not direct children of the <div> will not be targeted.
OR
Class Properties
When we call the House function, we assign the result to our variable,
which becomes an object.
house1=new Object();
house1.rooms=4;
house1.price=100000;
house1.garage=false;
We would have to type this in for all houses, which would be very
tedious and is why we use the class structure instead.
When we display the details for a house, I have introduced the ternary
operator, ‘?:’. The ternary operator is a compacted version of:
Class Methods
Much better!
Q:8
How you provide security for PHP application?
9
A:8 There are many ways to accomplish the security tasks but the most
9 common 7 ways are1. Validate Input. Never trust your user and
always filter input before taking it to any operation.2. Provide access
control.3. Session ID protection4. preventing Cross Site Scripting
(XSS) flaws
6. Turning off error reporting and exposing to the site for hackers.
Instead use log file to catch exceptions
Assume that this query is creating a new account. The user provides a
desired username and an email address. The registration application
generates a temporary password and emails it to the user to verify the
email address. Imagine that the user enters the following as a
username:
This certainly doesn’t look like a valid username, but with no data
filtering in place, the application can’t tell. If a valid email address is
given (shiflett@php.net, for example), and 1234 is what the
application generates for the password, the SQL statement becomes
the following:
<?php
$sql = "INSERT
INTO users (reg_username,
reg_password,
reg_email)
VALUES ('bad_guy', 'mypass', ''),
('good_guy',
'1234',
'shiflett@php.net')"; ?>
Q:9
What is cross site Scripting?
1
A:9 To understand what Cross Site Scripting is, let’s see a usual situation,
1 common to many sites. Let’s say we are taking some information
passed in on a querystring (the string after the (?) character within a
URL), with the purpose of displaying the content of a variable, for
example, the visitor’s name:
http://www.yourdomain.com/welcomedir/welcomepage.php?
name=John
As we can see in this simple querystring, we are passing the visitor’s
name as a parameter in the URL, and then displaying it on our
“welcomepage.php” page with the following PHP code:
<?php
?>
The result of this snippet is shown below:
http://www.yourdomain.com/welcomedir/
welcomepage.php?name=
<script language=javascript>alert
(‘Hey, you are going to be hijacked!’);</script>
Do you remember the PHP code included in our “welcome.php” page?
Yes, you’re correct. When we modify the querystring, the following
code is executed:
<?php
?>
The output of this code is an alert JavaScript box telling you “Hey, you
are going be hijacked!” after the “Welcome to our site” phrase.
Very ugly stuff, right? That’s a simple example of the Cross Site
Scripting vulnerability. This means that any pasted JavaScript code
into the URL will be executed happily with no complaints at all.
Q:9 Which method do you follow to get a record from a million
2 records? (Searching, …. not from database, from an array in
php)
A:9 use array_search(), array_keys(), array_values(),
2 array_key_exists(), and in_array().