How to Use String Function in PHP?

Friends, today we are going to tell you about the string function, when and where the string should be used, let us know about it, in PHP String is written in double quotes. Along with writing in double quotes(“”), you can also write string in single quotes(”).Let us understand through an example how we can use string in PHP. In the example given below, we store a string in a variable. We can display the string with the help of direct echo() function or we can also display it through variable.

Example:-

<?php
$str = "This is string ";
echo "String is collection of characters";
echo $str ;
?>

Output:-

String is collection of characters
This is string
Parameters :-

$str – a variable.
This is string -a string.
String is collection of characters– a string.
echo – echo() is a readymade function of PHP through which we can display the value.

PHP String functions:-

There are also some functions of string in PHP which are used to complete different types of tasks. Let’s talk about the PHP string function.

The Length of a String:-

The PHP strlen() function is used to find the length of a string. The example of strlen() function is as follows.

Example:-

<?php
echo strlen("Expertsquiz is a totorial website"); // outputs 33
?>

Str_words_count() function-

The PHP str_word_count() function is used to count the number of complete words in a string.

Example:-

<?php
echo str_word_count("Expertsquiz is a totorial website"); // output 4
?>

PHP strrev()-

The PHP strrev() function is used to reverse a complete string.

Example:-

<?php
echo strrev("Learn english")// outputs - hsilgne nraeL
?>

PHP str_replace() function –

The PHP str_replace() function is used to replace some characters with some other characters in a string. The example below can replace “English” with “Learn”.: In the example below, we’ll replace “Learn” with English:

Example:-

<?php
echo str_replace("Learn", "English", "Learn PHP "); // outputs English PHP !
?>

strpos() function :-

The PHP strpos() function is used to find the position of a text word (string) inside a string. In the example below, we can say “You learn english?” Will find the position of “english” in the string.

Example:-

<?php
echo strpos("You learn english ?", "english "); // outputs 11
?>

Strcmp() fuction:-

The PHP strcmp() function is used to compare two strings.

Example:-

<form action="" method="POST">
Name :<br>
<input type="text" id="text" name="name" placeholder="Your Text.."><br>
<br>
Compare :<br>
<input type="text" id="text" name="compare" placeholder="Your Text for compare"><br>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST["submit"]))
{
$name=$_POST['name'];
$compare=$_POST['compare'];
echo "Compared :";
echo strcmp("$name","$compare");
}
?>

Strtolower():-

The strtolower() function is used to convert a complete string to a lower case (small alphabet). ,

Example:-

<?php
$str="LEARN ENGLISH";
$res=strtolower($str);//for lower case
echo $res;
?//Output learn english
>

Strtoupper() Function :-

The strtoupper() function is in contrast to the string to lower function which is used to convert the full string to upper case (capital alphabet).

Example:-

<?php
$str="learn php tutorilas";
$res=strtoupper($str);//for upper case
echo $res;
?//Output LEARN PHP TUTORIALS
>