PHP String Functions Tutorial Example

php string, php data types

PHP STRING FUNCTION :

– PHP String is one type of data type which is used in programming language.

– Strings are use for store the data which contain set of characters.

– PHP supports set of 256-character.

– String is mainly used to represent text or character rather than numbers.

– Set of character is known as String. It also contains spaces and numbers.

– Quotation mark is used to define the string.

– We use single quote and double quote to define the string.

SINGLE QUOTE :

– A programmer create string by enclosing the text into single quote. This is easiest way to represent the string into php.

– Single quoted string are treated literally.

– When programmer wants as exactly string as defined into program then they can used this single-quote declaration for string.

– Into the declaration of string using single-quote if you want to display single quote then you have to use backslash(\). And if you want to show backslash then you have to use one more backslash means (\\).

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php  
   $myStr='Demonuts';  
   echo 'Welcome to $myStr';  
?>  
 
</body>
</html>

Below is the output of the above code :

Welcome to $myStr

In the above example, we define one variable $myStr. This variable have some value.Now we try to print the value of this variable by using single quote. single code will not take the value of variable but it print only variable name.

DOUBLE QUOTE :

– A string can also be defined using double-quote into PHP.

– PHP is enable to execute whole string when we use double quote.

– The main difference between single quote and double quote is that when we can include variable by using double quote. It will take value of variable.

– By using double quote, the escape sequence and variable will be generally be interpreted.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php  
   $myStr='Demonuts';  
   echo "Welcome to $myStr";  
?>  
 
</body>
</html>

Below is the output of the above code :

Welcome to Demonuts

In the above example, We define one variable $myStr having some value. Using double quote we got the value of variable as output.

Here are some escape-sequence replacements ..

– \ n will start your string with new line.

– \r replace by the carriage-return character

– \t replace by the tab character

– \$ replace by the $ sign

  \” replace with single double quote

STRING CONCATENATION :

– String concatenation means the operation of joining character of two string end-to-end.

– Use to combine two string variable together.

– We use (.) concatenation operator to combine two strings.

– Concatenation operator (.) will return the combination of right and left side arguments.

– (.=) is known as concatenation assignment operator, which append to right side arguments to left side argument.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php 

// First String 
$str1 = 'Hello'; 

// Second String 
$str2 = ' Demonuts!'; 

// Concatenation Of String 
$str = $str1.$str2; 

// print Concatenate String 
echo " $str \n"; 
?> 
 
</body>
</html>

Below is the output of the above code :

Hello Demonuts!

Also we can declare string as given below by using concatenation assignment operator.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php 

// First String 
$str = 'Hello'; 

// concatenation assignment operator 
$str .= ' Demonuts!'; 

// Contain whole String 
$result = $str; 

// print String 
echo " $result \n"; 
?> 
 
</body>
</html>

Below is the output of the above code :

Hello Demonuts!

STRING FUNCTIONS :

Here are some built-in string functions that we can use in our regular programs.

1. strlen() – This function returns the length of string. Mostly use for validating input field where user is bounded to enter limited characters.

EXAMPLE:-

<!DOCTYPE html>
<html>
<body>

<?php
echo strlen("DEMONUTS");
?> 
 
</body>
</html>

Below is the output of the above code :

8

2. str_word_count() – This function returns the numbers of words in a string.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo str_word_count("WEL COME TO DEMONUTS");
?> 
 
</body>
</html>

Below is the output of the above code :

4

3. strrev() – This function returns reverse string.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo strrev("HELLO DEMONUTS");
?> 
 
</body>
</html>

Below is the output of the above code :

STUNOMED OLLEH

4. str_replace() – This function replace characters with some other characters into string.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo str_replace("Website", "Demonuts", "Welcome to Website");
?> 
 
</body>
</html>

Below is the output of the above code :

Welcome to Demonuts

5. strpos() – this function use to search specific word in a string. If a search found it returns the position of first match otherwise returns FALSE.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo strpos("Welcome to Demonuts", "Demonuts");
?> 
 
</body>
</html>

Below is the output of the above code :

11

6. ucwords() – this function converts first alphabet into uppercase.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo ucwords("demonuts");
?> 
 
</body>
</html>

Below is the output of the above code :

Demonuts

7. strtoupper() – This function converts whole string into upper case.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo strtoupper("welcome to demonuts");
?> 
 
</body>
</html>

Below is the output of the above code :

WELCOME TO DEMONUTS

8. strtolower() – This function converts whole string into lower case.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo strtolower("WELCOME TO DEMONUTS");
?> 
 
</body>
</html>

Below is the output of the above code :

welcome to demonuts

9. str_repeat() – This function repeats string for specific number of times.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>

<?php
echo str_repeat("Demonuts:- ",5);
?> 
 
</body>
</html>

Below is the output of the above code :

Demonuts:- Demonuts:- Demonuts:- Demonuts:- Demonuts:-

10. trim() – This function removes white space and and predefined characters form both side of the string.

EXAMPLE :-

<!DOCTYPE html>
<html>
<body>
<?php
$str = "welcome to demonuts";
echo $str . "<br>";
echo trim("$str","welnuts");
?>
</body>
</html>

Below is the output of the above code :

welcome to demonuts
come to demo