PHP | Strings


Strings can be seen as a stream of characters. For example, ‘G’ is a character and ‘GeeksforGeeks’ is a string. We have learned about basics of string data type in PHP in PHP | Data types and Variables. In this article we will discuss about strings in details. Every thing inside quotes , single (‘ ‘) and double (” “) in PHP is treated as a string.

Creating Strings

There are two ways of creating strings in PHP:

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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 does not processes special characters. Hence, the string is unable to identify the ‘$’ sign as start of a variable name.

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Welcome to GeeksforGeeks
    Welcome to GeeksforGeeks
    

    In the above program we can see that the double-quote strings is processing the special characters according the 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 fix 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 single quote ignores the special characters but double-quoted strings recognize the special characters and treat them differently.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

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

  • The character beginning with a backslash(“\”) are treated as escape sequences and are 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.

Built-in String functions

Built-in functions in PHP are some existing library functions which 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 argument and return the length or number of characters in the string.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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 by second argument.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Hello WorldforWorld!
    Hello GeeksWorldGeeks!
    

    In the first example, we can see that all occurrences of the word “Geeks” is 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    llo Worl
    

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 contribute.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