Friday, April 9, 2010

difference between ksort and asort in php

Interview Question:

What is the difference between ksort and asort in php?

Answer:

ksort - sorts an array by its key
asort - sorts an array by its value

Example.

<?php
$resource = array("d" => "answers", "a" => "php", "b" => "interview", "c" => "questions");
ksort($resource);
foreach ($resource as $key => $val) {
echo "$key = $val\n";
}
?>

The sample output is

a = php b = interview c = questions d = answers

asort arsort diffrerences

Question: what is the difference between asort and arsort?

Answer:

asort - sorts an array. It maintains index values.

arsort- It sorts an array in reverse manner.

Example for asort:


<?php
$fruits = array("d" => "php", "a" => "interview", "b" => "questions", "c" => "answers");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

The output is c = answers a = interview d = php b = questions

Example for arsort:

<?php
$fruits = array("d" => "php", "a" => "interview", "b" => "questions", "c" => "answers");
arsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

The output is

b = questions d = php a = interview c = answers

question about array sorts in php

Question: What are all the array sorts in php?

Answer: The following are the array sort functions

sort()
asort()
arsort()
krsort()
usort()
ksort()
natcasesort()
rsort()
shuffle()
array_multisort()
uasort()
uksort()
natsort()

Thursday, April 8, 2010

extract domain name from the url using regular expression

Question: How to extract domain name from a url using regular expressions

Answer:

preg_match('@^(?:http://)?([^/]+)@i',"http://blogger.com", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";

The output is

domain name is: blogger.com

stristr trick questions

What is output of the following

echo stristr('user@EXAMPLE.com', 'R'); //
echo stristr('user@EXAMPLE.com', 'r'); //

Output

r@EXAMPLE.com
r@EXAMPLE.com

The stristr is case insensitive.

strstr trick questions

What is output of the following

strstr("php@interview",'@');
strstr("php@interview",'@',true);

Answer

@interview
php

This is the mostly asked interview question

substr - related questions

What is substr in php?

substr is used to return a part of a string

Two arguments can be passed to the substr

1. start value
2. length value

Examples
1. start value

substr('interview',1); // returns "nterview"
substr('interview',-2); //returns "ew"

2. Length value

substr('interview',-2,1); //the output is "e"
substr('interview',-3,-2); //the output is "i"
substr('interview',1,2); //the out put is "nt"

Your comment always welcome

Wednesday, April 7, 2010

captcha in php

Question: What is captcha in php

Answer

Captcha is nothing but an image which contains numbers,characters with an distorted background

Acronym for captcha is

Completely Automated Public Turing test to tell Computers and Human Apart

Simply saying : It is a mechanism for protecting spams in an application.






Tuesday, April 6, 2010

Explain Static keyword in php

Answer

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

what is abstract class in php

Interview question:

what is abstract class in php

Answer

Abstract classes are not allowed to create instance . We can only inherit the class using some other class.