Junal on the run

Some interesting PHP functions…

Posted on: February 9, 2008

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.

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 = “Junal is being tokenized”;

$tok = strtok($string, ” \n\t”);

while ($tok !== false) {

echo “Word=$tok<br />”;

$tok = strtok(” \n\t”);

}

Output :

Word=Junal
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 = ‘junal rahman’;

echo ucfirst($string);

output : “Junal rahman”

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” => “junal”, “anupom”, “manzil”, “ahsan”);

$array2 = array(“b” => “junal”, “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);

Excellent stuff! By the way, I have to go now. There are thousands thing I don’t know about PHP. Have to get idea about them, have to run ….

Reference : PHP manual.

3 Responses to "Some interesting PHP functions…"

Nice post….thanks
Please visit this blog for learn more PHP scripts….
http://amitmondal.wordpress.com

as salamoalaikom amer name balasi

superb……….

Leave a reply to mohammed Jahirul Islam Cancel reply

View Junal Rahman's profile on LinkedIn
Subscribe to me on FriendFeed

Follow Me on Twitter

Archives

My photos

Blog Stats

  • 428,791 hits