
String and Array Functions Examples in PHP
Learn how to compare strings, get string lengths, extract substrings, count occurrences, tokenize strings, split strings into arrays, and sort arrays in ascending and descending order in PHP. Explore array key functions and checking array keys. Check out examples and see how these functions work in practice.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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.
Function example output strcmp() echo strcmp("Hello world!","Hello world!"); 0 strcasecmp() echo strcasecmp("Hello world!","HELLO WORLD!"); 0 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
Function Example Output strtok() $string = "Hello world. Beautiful day today."; $token = strtok($string, " "); Hello world. Beautiful day today. while ($token !== false) { echo "$token<br>"; $token = strtok(" "); } str_split() print_r(str_split("Hello")); Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )
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
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
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 )