Skip to content
Related Articles

Related Articles

PHP | Strings
  • Difficulty Level : Easy
  • Last Updated : 05 May, 2021

Strings can be seen as a stream of characters. For example, ‘G’ is a character and ‘GeeksforGeeks’ is a string. We have learned about the basics of string data type in PHP in PHP | Data types and Variables.

 In this article, we will discuss strings in detail. Everything inside quotes, single (‘ ‘) and double (” “) in PHP is treated as a string. 
 

Creating Strings:
There are four ways of creating strings in PHP: 
 

1. Single-quote strings: This type of string does not process special characters inside quotes. 

PHP




<?php
 
// single-quote strings
 
$site  = 'Welcome to GeeksforGeeks';
 
echo $site;
  
?>

Output: 



Welcome to GeeksforGeeks

The above program compiles correctly. We have created a string ‘Welcome to GeeksforGeeks’ and stored it in variable and printing it using echo statement. 
Let us now look at the below program: 

PHP




<?php
 
// single-quote strings
 
$site  = 'GeeksforGeeks';
 
echo 'Welcome to $site';
  
?>

Output: 
 

Welcome to $site

In the above program the echo statement prints the variable name rather than printing the contents of the variables. This is because single-quotes strings in PHP do not process special characters. Hence, the string is unable to identify the ‘$’ sign as the start of a variable name. 
 

2.Double-quote strings : Unlike single-quote strings, double-quote strings in PHP are capable of processing special characters. 
 

PHP




<?php
 
// double-quote strings
 
echo "Welcome to GeeksforGeeks \n";
 
$site  = "GeeksforGeeks";
 
echo "Welcome to $site";
  
?>

Output: 

Welcome to GeeksforGeeks
Welcome to GeeksforGeeks

In the above program, we can see that the double-quote strings are processing the special characters according to their properties. The ‘\n’ character is not printed and is considered as a new line. Also instead of the variable name $site, “GeeksforGeeks” is printed. 
 

PHP treats everything inside double quotes(” “) as Strings.

 In this article, we will learn about the working of the various string functions and how to implement them along with some special properties of strings. Unlike other data types like integers, doubles, etc. Strings do not have any fixed limits or ranges. It can extend to any length as long as it is within the quotes. 
It has been discussed earlier that string with single and double quotes are treated differently. Strings within a single quote ignore the special characters, but double-quoted strings recognize the special characters and treat them differently. 



Example: 

PHP




<?php
  
$name = "Krishna";
echo "The name of the geek is $name \n";
echo 'The name of the geek is $name';
  
?>

Output: 

The name of the geek is Krishna 
The name of the geek is $name

Some important and frequently used special characters that are used with double-quoted strings are explained below: 

The character begins with a backslash(“\”) is treated as escape sequences and is replaced with special characters. Here are few important escape sequences. 
 

  1. “\n” is replaced by a new line
  2. “\t” is replaced by a tab space
  3. “\$” is replaced by a dollar sign
  4. “\r” is replaced by a carriage return
  5. “\\” is replaced by a backslash
  6. “\”” is replaced by a double quote
  7. “\'” is replaced by a single quote
  • The string starting with a dollar sign(“$”) are treated as variables and are replaced with the content of the variables.

3. Heredoc: The syntax of Heredoc (<<<) is another way to delimit PHP strings. An identifier is given after the heredoc (<<< ) operator, after which any text can be written as a new line is started.  To close the syntax, the same identifier is given without any tab or space. 

Note: Heredoc syntax is similar to the double-quoted string, without the quotes.

Example:

PHP




<?php
   
 $input  = <<<testHeredoc
 
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
I am enjoying this.
 
testHeredoc;
   
echo $input;
    
?>

Output:

Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
I am enjoying this.

4. Nowdoc: Nowdoc is very much similar to the heredoc other than the parsing done in heredoc.  The syntax is similar to the heredoc syntax with symbol <<< followed by an identifier enclosed in single-quote. The rule for nowdoc is the same as heredoc.

Note: Nowdoc syntax is similar to the single-quoted string.

Example:

PHP




<?php
 
$input = <<<'testNowdoc'
 
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
 
testNowdoc;
 
echo $input;
 
// Directly printing string without any variable
echo <<<'Nowdoc'
<br/>
Welcome to GFG .
Learning PHP is fun in GFG.
     
Nowdoc;
     
?>

Output:

Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.

Welcome to GFG .
Learning PHP is fun in GFG. 

Built-in String functions

Built-in functions in PHP are some existing library functions that can be used directly in our programs making an appropriate call to them. Below are some important built-in string functions that we use in our daily and regular programs: 

1. strlen() function: This function is used to find the length of a string. This function accepts the string as an argument and returns the length or number of characters in the string. 
 

Example: 

PHP




<?php
  
echo strlen("Hello GeeksforGeeks!");
  
?>

Output: 

20

2. strrev() function: This function is used to reverse a string. This function accepts a string as an argument and returns its reversed string. 

Example:  

PHP




<?php
  
echo strrev("Hello GeeksforGeeks!");
  
?>

Output: 

!skeeGrofskeeG olleH

3. str_replace() function: This function takes three strings as arguments. The third argument is the original string and the first argument is replaced by the second one. In other words, we can say that it replaces all occurrences of the first argument in the original string with the second argument. 

Example: 

PHP




<?php
  
echo str_replace("Geeks", "World", "Hello GeeksforGeeks!"), "\n";
echo str_replace("for", "World", "Hello GeeksforGeeks!"), "\n";
 
?>

Output: 

Hello WorldforWorld!
Hello GeeksWorldGeeks!

In the first example, we can see that all occurrences of the word “Geeks” are replaced by “World” in “Hello GeeksforGeeks!”. 

4. strpos() function: This function takes two string arguments and if the second string is present in the first one, it will return the starting position of the string otherwise returns FALSE. 

Example: 

PHP




<?php
 
echo strpos("Hello GeeksforGeeks!", "Geeks"), "\n";
 
echo strpos("Hello GeeksforGeeks!", "for"), "\n";
 
var_dump(strpos("Hello GeeksforGeeks!", "Peek"));
 
?>

Output: 

6
11
bool(false)

We can see in the above program, in the third example the string “Peek” is not present in the first string, hence this function returns a boolean value false indicating that string is not present. 

5. trim() function: This function allows us to remove whitespaces or strings from both sides of a string. 

Example: 

PHP




<?php
 
echo trim("Hello World!", "Hed!");
 
?>

Output: 

llo Worl

6. explode() function: This function converts a string into an array.

Example: 

PHP




<?php
  
  
$input  = "Welcome to geeksforgeeks";
   
print_r(explode(" ",$input));
    
?>

Output:

Array ( [0] => Welcome [1] => to [2] => geeksforgeeks )

7. strtolower() function: This function converts a string into the lowercase string.

Example: 

PHP




<?php
   
$input  = "WELCOME TO GEEKSFORGEEKS";
   
echo strtolower($input);
    
?>

Output:

welcome to geeksforgeeks

8. strtoupper() function: This function converts a string into the uppercase string.

Example:

PHP




<?php
   
$input  = "Welcome to geeksforgeeks";
   
echo strtoupper($input);
    
?>

Output:

WELCOME TO GEEKSFORGEEKS

9. strwordcount() function: This function counts total words in a string.

Example:

PHP




<?php
    
$input  = "Welcome to GeeksforGeeks";
   
echo str_word_count($input);
    
?>

Output:

3

10. substr() function: This function gives the substring of a given string from a given index.

Example:

PHP




<?php
  
$input  = "Welcome to geeksforgeeks";
   
echo(substr($input,3));
    
?>

Output:

come to geeksforgeeks

Recent articles on PHP Strings

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :