PHP String Functions

Notes:

PHP Strings & String Functions:
- Sequence of characters enclosed in between pair of double quotations or pair single quotations is considered as a string in PHP

$name1 = "Manjunath Chidre" ;
echo $name1;

$name2 = 'Manjunath Chidre' ;
echo $name2;

echo $name1[0]; // M
echo "<br/>";

strlen(value:string):int
- Returns the total number of characters in a given string
Ex: echo strlen($name1); // 16

strpos (value1:string,value2:string):int
- Returns the position of the first occurrence of the case sensitive value2 string in a given value1 string
Ex: echo strpos($name1,"M"); // 0

stripos (value1:string,value2:string):int
- Returns the position of the first occurrence of the case insensitive value2 string in a given value1 string
Ex: echo stripos($name1,"M"); //0

ord (value:string):int
- Returns the ascii value of the first character of the given string
Ex: echo ord('A'); // 65

chr(value:int):string
- Returns the equivalent character of the given ascii value
Ex: echo chr(65); // A

strtolower(value:string):string
- Returns a string with all uppercase characters converted to lowercase
Ex: echo strtolower($name1); // manjunath chidre

strtoupper(value:string):string
- Returns a string with all lowercase characters converted to uppercase
Ex: echo strtoupper($name1); // MANJUNATH CHIDRE

substr(value:string,startIndex:int,len:int):string
- Returns a substring consisting of the characters that start at the specified startIndex and with a length specified by len
Ex: echo substr($name1,0,9); // Manjunath

substr_replace(value1:string,value2:string,startIndex:int,len:int):string
- Returns a string by replacing the portion of the value1 by value2 delimited by startIndex and length
Ex: echo substr_replace($name1,"MASS",0,9); // MASS Chidre

explode(delimiter :string, value :string):array
- Splits or explodes a string into an array of substrings by dividing it wherever the specified delimiter occurs
Ex:
$parts = explode(" ",$name1);
echo $parts[0]; // Manjunath
echo "<br/>";
echo $parts[1]; // Chidre
echo "<br/>";

implode(delimiter :string, value :array):string
- Joins an array of strings in to a single string with a given delimiter
Ex:
$name3= implode(" - ",$parts);
echo $name3; // Manjunath - Chidre

trim(value:string):string
- Removes whitespaces from the beginning and end of the string
Ex:
echo "<pre>";
$name4 = " PHP ";
echo $name4; // PHP
echo "<br/>";
echo strlen($name4); // 7
echo "<br/>";
echo trim($name4); // PHP
echo "<br/>";
echo strlen(trim($name4)); // 4
echo "<br/>";
echo "</pre>";

ltrim(value:string):string
- Removes whitespaces from the beginning of the string
Ex:
echo "</pre>";
echo ltrim($name4); // PHP
echo "<br/>";
echo strlen(ltrim($name4)); // 5
echo "<br/>";
echo "</pre>";

rtrim(value:string):string
- Removes whitespaces from the end of the string
Ex:
echo "</pre>";
echo rtrim($name4); // PHP
echo "<br/>";
echo strlen(rtrim($name4)); // 5
echo "<br/>";
echo "</pre>";

Interview Questions:

1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a