When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i, and characters. A string can be made up of the following elements.
- Letters – A string can be made up of small letters (a – z) or capital letters (A – Z).
- Numbers – A string can contain any number from 0 to 9.
- Special Characters – A string can also contain special characters (@#$%^&!). Escape sequence characters are used to display special characters as strings.
Syntax:-
$string-name = "string";
PHP String Functions
There are many string functions already built into PHP. All these string functions are known as PHP readymade functions. We can also call the PHP String function a built-in function. We can use the functionality of the functions which are already created in PHP and by using them, we call such functions a built-in function or readymade function.
List of String Functions
- strlen() Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â To get the length of the string.
- Str_words_count()Â Â Â Â Â Â Â Â Â Â Â How many words are there in a string?
- PHP strrev()Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â To reverse the string.
- str_replace()Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â To convert strings.
- strpos():-Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â To get the position number of the word in the string.
- str_repeat                       The str_repeat() function repeats a string a specified number of times.
PHP strlen() function
<?php
echo strlen("Learn PHP strlen function"); // outputs 25
?>
String word count function: str_word_count() –
<?php
echo str_word_count("Learn PHP str_words_count function"); // output 34
?>
String reverse function: strrev()-
<?php
echo strrev("Learn strrev function")// outputs : noitcnuf verrts nrael
?>
String replace function: str_replace() –
<?php
echo str_replace("Hello", "HI", "Hello ,I am learning PHP");
// outputs:- HI ,I am learning PHP
?>
String position function: strpos():-
<?php
echo strpos("How are you ?", "You "); // outputs 8
?>
str_repeatÂ
<?php
echo str_repeat("How are you?", 4 );
// outputs : How are you?How are you?How are you?How are you?
?>