2012 php interview questions with answers

Posted by Unknown at 17:55
Ques: 1 Explain the difference between $message and $$message?
Ans
$message is used to store variable data. $$message can be used to store variable of a variable. Data stored in $message is fixed while data stored in $$message can be changed dynamically.
E.g. $var1 = ‘Variable 1’
$$var1= ‘variable2’
This can be interpreted as $ Variable 1=‘variable2’;
For me to print value of both variables, I will write
$var1 $($var1)

Ques: 2 What is session hijacking?

Session hijacking is the misuse of a valid computer session. It is used to attain unauthorized and illegal access to a system. This access is attained using the “brute force” attack where in he tries multiple id’s to login in a system while the session is in progress. The most common method of session hijacking is IP spoofing where an attacker uses source-routed IP packets to insert commands into an active communication between two systems on a network and pretending itself as one of the authenticated users.


Ques: 3 How session will work when we disable cookies of client browser?
Ans:
Session is kind of cookie who is work on server side.For working of session on server side cookies should be enable on server side and also on client side browser.But session will also work when cookies are disable on client side by using URL session passing.
Ques: 4 How we use array_search() in PHP?
Ans:
When we use array_search() function if it found the particular value than it will return index corresponding to array value.
Example:
<?php
$Emp_name_array = array(0 => 'vivek', 1 => 'umang', 2 => 'shrish', 3 => 'jalees');
$key = array_search('umang', $Emp_name_array);
// return $key = 1;
$key = array_search('vivek', $Emp_name_array);   // return $key = 0;
?>
Ques: 5 How you differentiate among sort(),assort() and Ksort()?
Ans:
1) sort()
This function sorts an array. Elements will be arranged
from lowest to highest when this function has completed.

<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
----------------------------OUTPUT---------------------
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
-------------------------------------------------------
2) asort()
This function sorts an array such that array indices
maintain their correlation with the array elements they are
associated with. This is used mainly when sorting
associative arrays where the actual element order is
significant.
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" =>
"banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

--------------------OUTPUT------------------------
c = apple
b = banana
d = lemon
a = orange
--------------------------------------------------

3) ksort()
Sorts an array by key, maintaining key to data
correlations. This is useful mainly for associative arrays.

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana",
"c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

--------------------OUTPUT----------------------------
a = orange
b = banana
c = apple
d = lemon
------------------------------------------------------
Ans:
The sort() function sort the array. Element will arranged in lowest to highest when function has completed.
The assort() function sort the associative arrays and where the actual element order is significant.

The Ksort() function sort an array by key. This is useful for associative array.
Ans:
Sort-sorting arry

<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
The above example will output:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

Assort-Sort an array and maintain index association
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
The above example will output:
c = apple
b = banana
d = lemon
a = orange
ksort — Sort an array by key
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
The above example will output:
a = orange
b = banana
c = apple
d = lemon
Ans:
Sort-sorting arry

<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
The above example will output:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
Assort-Sort an array and maintain index association
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
The above example will output:
c = apple
b = banana
d = lemon
a = orange
ksort — Sort an array by key
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
The above example will output:
a = orange
b = banana
c = apple
d = lemon

Ans:
sort() -> sorting the value in ascending.d
assort() -> sorting only the value.
Example: $x=array([0]=>32,[1]=>11,[2]=>10)
 print_r(asort($x); means: [2]=>10,[1]=>11,[0]=>32
Ksort() -> Sorting only key not value
Ans:
In array we can get both key value and array value.key is refer for index,array is what we are storing.sort() use to sort in ascendending array value.arsort() user array in reverse order.
ksort means key  valuesare arranged in ascending order
Ans:
sort() will order the array by its values without preserving the keys. Use it when array is indexed numerically or when you do not care about the keys.
asort() will also sort the array by its values, but it will preserve the key -> value association.
The ksort() function sorts an array by the keys. The values keep their original keys.This function returns TRUE on success, or FALSE on failure.
Ans:
The sort() function sorts an array by the values.This function assigns new keys for the elements in the array. Existing keys will be removed.This function returns TRUE on success, or FALSE on failure.
The asort() function sorts an array by the values. The values keep their original keys.
This function returns TRUE on success, or FALSE on failure.
The ksort() function sorts an array by the keys. The values keep their original keys.
This function returns TRUE on success, or FALSE on failure.
Ans:
bool sort ( array &$array [, int $sort_flags] )
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.Returns TRUE on success or FALSE on failure.
Example 329. sort() example
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
The above example will output:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
Ans:
bool sort ( array &$array [, int $sort_flags] )
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.Returns TRUE on success or FALSE on failure.


Example 329. sort() example

<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
The above example will output:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
Ans:
sort()
void sort(array target_array [, int sort_flags])
The sort() function sorts the target_array, ordering elements from lowest to highest value.Note that it doesn’t return the sorted array. Instead, it sorts the array “in place,” returningnothing, regardless of outcome. The optional sort_flags parameter modifies the function’s default behavior in accordance with its assigned value:
• SORT_NUMERIC: Sort items numerically. This is useful when sorting integers or floats.
• SORT_REGULAR: Sort items by their ASCII value. This means that B will come before a, for instance. A quick search online will produce several ASCII tables.
• SORT_STRING: Sort items in a fashion that might better correspond with how a human
might perceive the correct order. See natsort() for further information about this matter, introduced later in this section.
Consider an example. Suppose you wanted to sort exam grades from lowest to highest:
$grades = array(42,57,98,100,100,43,78,12);
sort($grades);
print_r($grades);
The outcome looks like this:
Array ( [0] => 12 [1] => 42 [2] => 43 [3] => 57 [4] => 78 [5] => 98
[6] => 100 [7] => 100 )
It’s important to note that key/value associations are not maintained. Consider the
following example:
$states = array("OH" => "Ohio", "CA" => "California", "MD" => "Maryland");
sort($states);
print_r($states);
Here’s the output:
Array ( [0] => California [1] => Maryland [2] => Ohio )
Ans:
sort destroy key association asort keeps key association of values. ksost sort the key value only
Ques: 6 How to print \ in php with out using . or *?.
Ans:
I have given you a example which definitely solved your query.
Example:
<?php
print "\\";
?>
output:
\\
Ans:
<?php
    echo "\\";
?>
Ans:
\ for slah and * for adding values
Ques: 7 Why is PHP-MySQL used for web Development?
Ans:
We use PHP-MySQL in web development because both are open source means both free to use. Both when MySQL work with PHP it gives result much faster than when MySQL work with Java/.Net/Jsp.   
Ques: 8 What function we used to change timezone from one to another ?
Ans:
I have given you a function using them we can change a timezone into another timezone.
date_default_timezone_set() function
Example:
date_default_timezone_set('India');
And we use date_default_timezone_get() function to get the entered date.
Ques: 9 How do you understand about stored procedure,Triggers and transaction in php?
Ans:
Below I have given how stored procedure,Triggers and transaction work in PHP:
1.Stored Procedure: It is combination of some SQL commands it is stored and can be compiled.Suppose that when user is executed a command than user has don't need to reissue the entire query he can use the stored procedure.Basically main reason to use the stored procedure is that using this we can decrease the time of reissue of that query. Because work done on server side is more than the work done on application side.
2.Trigger: Trigger is a stored procedure.This is used to work stored procedure effectively.It is invoked when a special type of event comes.For effectively understand this i have given you a example.
When we attach a trigger with stored procedure than when we delete a record from transaction table than will automatically work and delete the respective client from client table.
3.Transaction: Transaction is that in which particular amount of data goes from the a client to the another client.We maintain the transaction record into a table called transaction table.    
Ques: 10 How we can upload videos using PHP?
Ans:
When want to upload videos using PHP we should follow these steps:
1.first you have to encrypt the the file which you want to upload by using "multipart-form-data" to get the $_FILES in the form submition.
2.After getting the $_FILES['tmp_name'] in the submition
3.By using move_uploaded_file (tmp_location,destination) function in PHP we can move the file from original location.
Ques: 11 How can we create a database using PHP and myqsl?
Ans:
I have given you a example using this you can create a database.
<?php
$con =mysql_connect("localhost","vivek","vivabh");
if (!$con)
  {
     die('Could not connect: ' . mysql_error());
  }
if (mysql_query("CREATE DATABASE my_db",$con))
  {
     echo "Database has been created";
  }
else
  {
     echo "Error to create database: " . mysql_error();
  }
mysql_close($con);
?>
Ques: 12 What do you understand about Joomala in PHP?
Ans:
Joomala is an content management system who is programmed with PHP.Using this we can  modified the PHP sit with their content easily.
Ques: 13 How you can differentiate abstract class and interface?
Ans:
There are some main difference between abstract and interface are given below:
1.In an abstract we can use sharable code where in the case of interface their is no facility to use sharable code.
2.In which class we implement an interface class. All method that we use should be defined in interface. Where as class those extending an abstract while a class extending an abstract class their is no need to defined methods
in the abstract class.
Ques: 14 What do you understand about Implode and Explode functions?
Ans:
The main difference b/w Implode and Explode functions is that We use Implode function to convert the array element into string where each value of array is separated by coma(,).
Example:
<?php
$array = array('First_Name', 'Email_Id', 'Phone_NO');
$comma_separated_string = implode(",", $array);
echo $comma_separated_string;
?>
output:
First_Name,Email_Id,Phone_No
 We use Explode function to convert the string value those are separated with coma(,) into  array.
Example:
<?php
$comma_separated_string = string('First_Name,Email_Id,Phone_No');
$array = Explode(",", $coma_separated_string);
echo $array;
?>
output:
First_Name
Email_Id
Phone_No
Ques: 15 Can we submit a form without submit button?
Ans:
Using JavaScript we can submit a form without submit button.
Example:
document.FORM_NAME.action="Welcome.php";
document.FORM_NAME.submit();//this is used to submit the form
Ques: 16 Can I develop our own PHP extension?if yes,how?
Ans:
Yes,we can develop a PHP extension like that,
Example:
<?
echo 'PHP';
?>
 To save this file as .php extention
Ques: 17 What do you understand by nl2br()?
Ans:
nl2br() function is stands for new line to break tag.
Example:
echo nl2br("R4R Welcomes\nYou");
output:
R4R Welcomes
you
Ques: 18 How function strstr and stristr both work in PHP?
Ans:
Generally, Both function strstr and stristr are same except one thing stristr is a case sensitive where as strstr is an non-case sensitive.We use strstr to match the given word from string.
Syntax:
syntax:
strstr(string,match word)
Examle:
<?php
$email_id = 'abc@xyz.com';
$id_domain = strstr($email_id, '@');
echo $id_domain;
?>
output:
@xyz.com
Ques: 19 In PHP can I get the browser properties?
Ans:
Using get_browser function we can get the browser properties in PHP.
Example:
$browser_properties = get_browser(null, true);
print_r($browser_properties);
Ques: 20 How we can increase the execution time of a PHP script?
Ans:
I have given you some function using them you can increase the execution time of a PHP script.Default time for execution of a PHP script is 30 seconds.
These are,
1.set_time_limit()//change execution time temporarily.
2.ini_set()//change execution time temporarily.
3. By modifying `max_execution_time' value in PHP
configuration(php.ini) file.//change execution time permanent.

Ques: 21 How we can get the value of current session id?


Ans:
We can get the value of our current session id by using session_id().It returns the value of our current session id.


Ques: 22 How we use ereg_replace() and eregi_replace()in PHP?


Ans:
The main difference b/w ereg_replace and eregi_replace() is that,eregi_replace is case sensitive. Example:In eregi_replace() it think both \'welcome\' and \'WeLCome\' are different. where as ereg_replace() is not case sensitive.

Example:In ereg_replace() it think both \'welcome\' and \'WeLCome\' are same.
Ques: 23 Which type of inheritance exists in PHP?


Ans:
PHP supports Multi-level inheritance.It doesn't support multiple inheritence.But using interface  we can achieve multiple inheritance in PHP.

Ques: 24 What do you understand about pear in PHP?


Ans:
Basically PEAR is stands for PHP Extension And Repository.PEAR is a framework and distribution system using that we can make PHP component reusable.It has huge collection of different classes.We use this for advance scripting.Example: Database,Mail,HTTP etc.

Ques: 25 Tell me how to use COM components in PHP?


Ans:
We use COM component in PHP by using that manner,


Example:
<?php
$objCom = new COM(�AddNumber.math�);
$output = $objCom ->AddTwoNum(4,5);
echo $output;
?>
Ques: 26 Tell me default session time in PHP and how can I change it?


Ans:
The default session time in PHP is 14400.We can change it like that manner,


Synatx:
$session_time_Limit="14400";
ini_set(session.gc_maxlifetime,$session_time_Limit);
 
Ques: 27 How we use copy() and move() file uploading functions in PHP? 


Ans:
When we use copy function it copy the file from source location to destination location and keep original file to the source location.


Syntax:
copy($_FILES['uploadedfile']['tmp_name'], $destination_path)
Where as when we use move function it copy the file from source location to destination location and delete original file from source location.
Syntax:
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $destination_path)
Ques: 28 How can we find out the name of the current executing file?


Ans:
I have given you a example that gives you the name of current executing file,


Example:
<?php
$File_Name = $_SERVER["SCRIPT_NAME"];
echo $File_Name;
?>
Ques: 29 What is the main difference b/w include_once() and require_once() in PHP?


Ans:
The main difference b/w include_once() and require_once() is that, In PHP if file is not exist and specified with include_once() it gives show warning message.Where as if we specified file as require_once() it giver a fatal error. 

Ques: 30 what are the constant value in PHP?can we write them without $ symbol? 


Ans:
We can't change the value of constant.Yes,we can write constant value in PHP.Below I have given you a example:
define("CONSTANT","R4R Welcomes You");
echo "CONSTANT";

output:
R4R Welcomes You


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

Blog Archive

 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top