Recently,
I have studied three types of PHP functions and they are: String functions,
Date functions and Array functions. I have noted down some functions that were
interesting. Well, PHP got thousands of built in functionality and I guess this
is what makes PHP great. So, let’s have a look at some interesting functions
from my collection
String functions:
strpbrk ()
-
Search a string for any of a set of characters
Example :
$text = ‘Junal is studying
php functions’;
echo strpbrk($text, ‘s’);
What will be the output? Well its will output: “studying php function”, because‘s’ is matched first.
What will be the output? Well its will output: “studying php function”, because‘s’ is matched first.
strrev()
-
This will reverse the given string
Example:
echo strrev(“junal likes to
be lost”);
output : “tsol eb ot sekil
lanuj” – funny eh ?
strtok()
-
Tokenizing a string
-
Example :
$string = “rupesh is being
tokenized”;
$tok = strtok($string, ”
\n\t”);
while ($tok !== false) {
echo “Word=$tok<br />”;
$tok = strtok(” \n\t”);
}
Output :
Word=rupesh
Word=is
Word=being
Word=tokenized
Word=is
Word=being
Word=tokenized
ucfirst()
-
Ahh ! This is one of the most interesting and needy one that I used currently
one of out facebook application, it uppercase the first letter of first word!
Example:
$string = ‘Rupesh’;
echo ucfirst($string);
output : “Rupesh”
ok this above function make
first letter of first word uppercase right. How about if we want all word’s
first letter to be uppercase? Simple! We just have to use ucwords() function !
Date Functions:
Well, among all date
functions I found one date function interesting
idate()
-
Format a local time/date as integer
Example:
$timestamp = strtotime(’9th
February 2008′);
echo idate(‘m’, $timestamp);
Output: “2″
Array Functions
Array_diff()
-
Computes the difference of arrays.
-
Example :
$array1 = array(“a” => “rupesh”,
“anupom”, “manzil”, “ahsan”);
$array2 = array(“b” => “rupesh”,
“anupom”, “manzil”);
$result = array_diff($array1,
$array2);
print_r($result);
output : “ahsan”
array_key_exists ()
-
Checks if the given key or index exists in the array
array_multisort ()
I used this function in
several occasions. Very handy function to sort the multiple arrays. It does a
lot of help when we want to sort an array by rank/number.
array_product()
-
Calculate the product of values in an array
Example:
$a = array (2, 4, 6, 8);
echo $a;
output :” 384″
natcasesort ()
-
Sort an array using a case insensitive “natural order” algorithm
range ()
-
Create an array containing a range of elements
Well that’s all! After
studying these array functions, I have found another interesting thing about
ArrayObject.
$array = array(’1′ =>
‘one’,
’2′ => ‘two’,
’3′ => ‘three’);
$arrayobject = new
ArrayObject($array);.