String Function in PHP

A string function in PHP includes different string operations like:

String Concatenation in PHP

A string concatenation in PHP is used to join two strings. And we use a “.” for it.

<!DOCTYPE html>
<html>
<body>

<?php
   $txt="My name is: ";
   $name="Ali";
   
   echo $txt . " " . $name;
?> 
</body>
</html>

strlen( ) Function in PHP

strlen( ) function in PHP is used to return the length of a string.

<!DOCTYPE html>
<html>
<body>

<?php
echo strlen("Here you go!");
?> 
 
</body>
</html>

strpos( ) Function in PHP

To find out the position of string or character in a string, a strpos( ) function in PHP is used.

  • If you enter the string or character that is available in the string then it displays the exact position.
  • But, if you entered a string or character that is not present in the string, then it will gives you an error.
<!DOCTYPE html>
<html>
<body>

<?php
echo strpos("PHP Language", "Language");
?> 
 
</body>
</html>

str_word_count( ) Function in PHP

A str_word_count( ) is a function that is used to the total words in a string.

<!DOCTYPE html>
<html>
<body>

<?php
echo str_word_count("Zara here");
?> 
 
</body>
</html>

str_replace( ) Function in PHP

To replace some characters with others in a string, we use str_replace( ) function in PHP.

<!DOCTYPE html>
<html>
<body>

<?php
echo str_replace("My", "Our", "My Language");
?> 
 
</body>
</html>

strrev( ) function in PHP

An strrev( ) function in PHP is normally used to reverse a string.

<!DOCTYPE html>
<html>
<body>

<?php
echo strrev("My world!");
?> 
 
</body>
</html>