Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Function.

Similar presentations


Presentation on theme: "PHP Function."— Presentation transcript:

1 PHP Function

2 String Function Function strcmp()
Compare two strings (case-sensitive): strcasecmp() Compare two strings (case-insensitive): strlen() returns the length of a string substr() function returns a part of a string  substr_count() counts the number of times a substring occurs in a string strtok() splits a string into smaller strings (tokens). str_split() The str_split() function splits a string into an array.

3 Function example output strcmp() echo strcmp("Hello world!","Hello world!"); strcasecmp() echo strcasecmp("Hello world!","HELLO WORLD!"); strlen() echo strlen("Hello"); 5 substr() echo substr("Hello world",6); echo substr("Hello world",10)."<br>"; echo substr("Hello world",1)."<br>" World d ello world  substr_count() echo substr_count("Hello world. The world is nice","world"); 2

4 Function Example Output strtok() $string = "Hello world. Beautiful day today."; $token = strtok($string, " "); while ($token !== false) { echo "$token<br>"; $token = strtok(" "); }  Hello world. Beautiful day today. str_split() print_r(str_split("Hello")); Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )

5 Array Function Function sort() sort arrays in ascending order rsort()
- sort arrays in descending order count() returns the number of elements in an array. array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.  array_keys() function returns an array containing the keys

6 Function example Output sort() $cars = array("Volvo", "BMW", "Toyota"); sort($cars); $clength = count($cars); for($x = 0; $x <  $clength; $x++) {      echo $cars[$x];      echo "<br>";} BMW Toyota Volvo rsort() $cars = array("Volvo", "BMW", "Toyota"); rsort($cars); $clength = count($cars); for($x = 0; $x <  $clength; $x++) {      echo $cars[$x];      echo "<br>";} Volvo Toyota BMW count() $cars=array("Volvo","BMW","Toyota"); echo count($cars); 3

7 Function example Output array_key_exists() $a=array("Volvo"=>"XC90","BMW"=>"X5"); if (array_key_exists("Volvo",$a))    {    echo "Key exists!";    } else    {    echo "Key does not exist!";    } Key exists  array_keys() $a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a)); Array ( [0] => Volvo [1] => BMW [2] => Toyota )


Download ppt "PHP Function."

Similar presentations


Ads by Google