Showing posts with label beginners. Show all posts
Showing posts with label beginners. Show all posts

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

What is array in php

Question: What is array in php and how to create an array in php?

Answer: In php array is an ordered map. It contains values and keys.

To create an array we need to use array keyword

Example:

$a= array('php','interview','questions','answers');

This creates simple array.

what are the frameworks available in PHP

Answer

Zend framework
cake php
prado
symfony
codeigniter
akelos
Yii

etc..,

Disadvantages of PHP?

Answer

1. It is a loosely typed language.
2. Multi threading is possible
3. PHP lacks in performance while comparing with java.(thats why very big applications are desinged using java)
4. It lacks in security.

Advantages of PHP

Answer

1. Good performance.
2. High scalability , reliability.(Ex. facebook uses php)
3. Easy to learn
4. High speed development
5. Availability of frameworks
6.Lot of Content management systems
7. It is a open source
8. It supports most of the operating systems(windows,linux,solaris etc)
9. PHP supports most of the databases(mysql,oracle,postgresql etc..)
10. Even support java classes.

Sunday, April 4, 2010

what is php?

This question is most common for freshers and below 6 month experienced candidates only

Answer

PHP - acronym - hypertext preprocessor

Php is a opensource scripting language generally used for rapid web application development

Saturday, April 3, 2010

How to unset a reference

Answer

Use unset

$a = 1;
$b =& $a;
unset($a);

This will not unset $b;

difference between Reply-to and Return-path in mail header

Answer

Reply-to: Reply-to is where to delivery the reply of the mail.


Return-path: Return path is when there is a mail delivery failure occurs then where to delivery the failure notification.

what is drupal?

Answer

Drupal is a free software package that allows an individual or a community of users to easily publish, manage and organize a wide variety of content on a website. Tens of thousands of people and organizations areusing Drupal to power scores of different web sites


Friday, April 2, 2010

differences between GET and POST methods

When we submit a form, which has the GET method it displays pair of name/value used in the form at the address bar of the browser preceded by url. Post method doesn't display these values.

differentiate __sleep and __wakeup?

Answer


__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.

What Is a Persistent 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.

what is cookies in php

Answer

Say simply - - Cookies are mechanism of storing temporary datas in browser memory