April 10, 2010

Important Functions of PHP


A function is a set of actions defined by a user, which affects one or more objects as per the definition. The function block starts with the function keyword. Functions, once created, can be reused in other parts of the document. The syntax for defining a function is as follows:

function functionname()
{
  ..
  ....
  .......
}

Some important function are listed below:

array_slice() function

The array_slice() function is used to extract a large piece of an array. The syntax of the array_slice() function is as follows:

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys = false ]] )

the parameter can be defined as,


Option


Description

array

It is the input array.

offset

If offset is positive, the sequence starts from that offset in the array. If offset is negative, the sequence starts from the end of the array.

length

If the length is omitted, array_slice() assumes that the user wants all elements from the starting position onward returned.

preserve_keys

When this option is used, array_slice() preserves the keys and does not reorder them.

array_shift() function

The array_shift() function is used to remove and return the first element of an array passed to it as an argument. It is used mainly to create a queue and act on it until the queue is empty. The array_shift() function either returns the shifted value of an array, or returns the NULL value if array is empty or argument passed is not an array.

array_merge() function
The array_merge() function is used to merge two or more arrays by combining all their elements. If the input arrays have the same index keys, the later value for that key overwrites the previous one. If arrays contain keys in numeric form, the later value for that key appends the index keys instead of overwriting it.

array_intersect() function
The array_intersect() function extracts all the elements that are common for two or more arrays. Since this function only checks whether the values are the same, the index keys of arrays are ignored. Let's take an example to understand this:

<?php
$array1 = array ('a' => 20, 30, 35);
$array2 = array ('b' => 20, 35, 30);
$array = array_intersect($array1, $array2);
var_dump ($array);
? >

The output of the above script will be array(3) { ["'a'"]=> int(20) [0]=> int(30) [1]=> int(35) } since three elements (20, 30, 35) are common in both arrays, i.e., $array1 and $array2.

sort() function
The sort() function is used to sort a given array either alphabetically if any strings are present or numerically if all elements are numbers. However, a user cannot pass an associative array to the sort() function. The reason behind this is that the values of the array are sorted as expected but keys are replaced by numerical indices that follow the sort order.

asort() function
The asort() function accepts an associative array and sorts its values either alphabetically if any strings are present or numerically if all elements are numbers. However, the asort() function preserves the array keys.

ksort() function
The ksort() function accepts an associative array and sorts its keys either alphabetically if any strings are present, or numerically if all elements are numbers. However, it does not sort the elements of the associative array.



 
Share/Bookmark

0 comments:

Post a Comment