PHP interview question and answer

Back
Now Lets talk about some functions.
Do you know about array_shift function()?
Yes, array_shift() removes the first element from an array.
<?php
$array = array("Cat", "Rat", "Bat");
// An array with three elements.
$arr = array_shift($array);
print_r($arr);
?>
O/P will be
[0]=> Rat
[1]=> Bat


Do you know about array_unshift function()?
array_unshift() insert one or more element to the biggining of an array.
<?php
// An array with two elements.
$array = array("Rat", "Bat");

$arr = array_unshift($array, "Cat", "Hat");
print_r($arr);
?>
O/P will be
[0]=> Cat
[1]=> Hat
[2]=> Rat
[3]=> Bat


What is array_pop function()?
This fuction delete the last element of an array.
<?php
// An array with four elements.
$array = array(
"Cat", "Hat", "Rat", "Bat");
$arr = array_pop($array);
print_r($arr);
?>
O/P will be
[0]=> Cat
[1]=> Hat
[2]=> Rat

What is array_push function()?
This fuction inserts one or more elements to the end of the array.
<?php
// An array with four elements.
$array = array(
"Cat", "Hat", "Rat");
$arr = array_push($array
, "Bat");
print_r($arr);
?>
O/P will be
[0]=> Cat
[1]=> Hat
[2]=> Rat
[3]=> Bat
What is array_unique function in PHP?
array_unique() remove duplicate values from an array.
For example:
<?php
//Assign an array with three elements.
$array = array(
"Cat", "Bat", "Cat");
$arr = array_unique($array
);
print_r($arr);
?>
O/P will be
[0]=> Cat
[1]=> Bat

What is array_slie function in PHP?
array_slice() extract the slice of an array.
For example:
<?php
//Assign an array with some elements.
$array = array(
"A", "B", "C", "D", "E");
$arr_1 = array_slice($array, 2);
$arr_1 = array_slice($array, 0, 3);
print_r($arr_1);
print "<br>";
print_r($arr_2);
?>
O/P will be
[0]=> C
[1]=> D
[2]=> E

O/P will be
[0]=> A
[1]=> B
[2]=> C

What is array_sum() function?
array_sum function calculate and return the sum of an array.
For example:
<?php
//Assign an array.
$array = array(
1, 3, 5, 7);
echo array_sum($array
);
?>
O/P will be
16
What is the difference between ECHO() and PRINT() ?
Print is a function where Echo is a statment. We can say that print is a functionnot a construct and allow such a advantage of formatted output.
Another major difference is that print does not take multiple exprations like if we write print "Hi", "Quizplaza"; then it will create a Parse Error. But in the case of echo "Hi", "Quizplaza"; It will execute.
Print returns boolan value (T or F). Echo doesn't return value.

What is different between GEt and Post?
In POST method, Data is sent by standered input. (That means nothing show in the url when posting a form.)
In the case of GET method, Data is sent through query strring.(That means data will be visibil for a user in the url.) Post is much secure and we cab send lot more data than that of Get.
We can send only 255 charector with the GET method.

What is SCOPE RESOLUTION OPERATOR?
It indicates with double colon (::). It is a token that allows access to static content and overridden members or method of a class.
Concider the following example. There is class A and class B.
<?php
class {
    function 
exam() {
        echo 
"I am function A::exam().<br />n";
    }
}

class 
extends {
    function 
exam() {
        echo 
"I am the redefined function B::exam().<br />n";
        
A::example();
    }
}

// there is no object of class A.
// O/P -  I am the original function A::example().<br />
A::exam();

$b = new B;
// Just create an object of class B.

// O/P-   I am the redefined function B::example().<br />
// O/P-   I am the original function A::example().<br />
$b->exam();
?>

 
What is the difference between trim(), rtrim() and ltrim() ?
TRIM() funtion returns a string with whitespace stripped from the bigining and end of the string.
Syentax = string trim ( string str [, string charlist] )
where string charlist is an optional value. The stripped characters can also be specified using the charlist parameter.


<?php
// Initialize a string.

$string = "tt This is a string ....  ";

echo trim($string);
?>
O/P will be
This is a string ...

rtrim() funtion returns a string with whitespace stripped from end of the string.
Syentax = string rtrim ( string str [, string charlist] )
where string charlist is an optional value. The stripped characters can also be specified using the charlist parameter.

 
<?php
// Initialize a string.

$string = "tt This is a string ....  ";

echo rtrim($string);
?>
O/P will be
tt This is a string ...

ltrim() funtion returns a string with whitespace stripped from the biggining of the string.
<?php
// Initialize a string.
$string = "tt This is a string ....  ";

echo ltrim($string);
// Another example is.
echo ltrim($string, "t.");
?>
O/P will be
This is a string ...
This is a string ...

Do you know about PHP_UNAME()?
Yes, PHP_UNAME function returns the information about the operating system where the PHP is running.
Syntext = string PHP_UNAME([string mode])

<?php
echo php_uname();
?>
O/P May be
Linux gains.domainsname.com 2.4.19-394.3.1.el5.lve0.8.44 #1 SMP Mon Oct 3 18:27:54 EEST 2011 x86_64
What is DOM ?
The DOM (Document Object Model) is the API (Application Programming Interface) that binds javascript and other scripting languages together with HTML (Hypertext Marckup Language) and other markup languages.

What is the difference between explode() and implode() ?
A exploed function splits a string into substrings by string seprator and retyrn array of string.
Syntex = array explode (string seprator, string string [, int limit])
For example:
<?php
//Assign a string.
$str = 'quiz1 
quiz2 quiz3 quiz4';
$expstr = explode($str
);
echo $
expstr[0];
echo $expstr[1];
echo $expstr[2];
echo $expstr[3];
?>
O/P will be
quiz1
quiz2
quiz3
quiz4

Where implode function join array elements with a string.
Syntex = string implode (string seprator, string string [, int limit])
For example:
<?php
//Assign an array.
$array = ('quiz1', '
quiz2', 'quiz3', 'quiz4');
$imp = implode(', ', $array
);
echo $
imp;
?>
O/P will be
quiz1, quiz2, quiz3, quiz4



Share your requirement with me and give your business a much needed thrust.
CONTACT HERE