PHP Strings

In this PHP tutorial, you will learn all about the Strings in PHP. We will discuss in detail Strings and various types of String Functions.

What is meant by Strings in PHP?

The string data type is the sequences of alphanumeric characters. There is basically a non-numeric data type. A string can be letters, words, numbers, and special characters. While assigning a string to a variable they are considered the same but it is different in some cases. There are mainly four ways to specify a string there are namely single-quoted, double-quoted, heredoc syntax, and newdoc syntax

Different types of Strings in PHP

  1. Single quoted
  2. Double quoted
  3. Heredoc syntax
  4. Newdoc syntax

What is a Single quoted String in PHP?

In PHP, we can generate a string by wrapping the string in a single quote. It is the most convenient way to define a string in PHP. We can either assign the string value to a variable and print the string or we can directly print the string using the printing functions.


echo 'Hello world! ';
$str = 'learnetutorials.com';
echo $str;
 

Output:


Hello world! learnetutorials.com

In the above example we can see that we created the string by enclosing the string in single-quote. The strings in the single quote will be directly printed. We can’t use the variable or any other escape sequences inside the single-quote. If we want to use the single-quote inside the string we have to use a backslash (\) before the quote otherwise it will throw an error.

Example for using the single quote in the string


$str = 'Welcome to \'learnetutorials.com\' ';
echo $str;
 

Output:


Welcome to 'learnetutorials.com'

What is a Double quoted String in PHP?

In PHP, we can also generate a string by wrapping the string in a double quote. Similar to single-quoted strings we can either assign the string value to a variable and print the string or we can directly print the string using the printing functions. In the double-quoted strings, we can also use the variable or other escape sequences. 


$str = 'learnetutorials.com';
echo "Welcome to $str";
 

Output:


Welcome to learnetutorials.com

In the above example, we can see that we created the string by enclosing the string in double-quotes. We have also used the variable in the double-quoted string.

The escape sequences used in the double-quoted strings are:

  • “\n” is replaced by a new line
  • “\t” is replaced by a tab space
  • “\$” is replaced by a dollar sign
  • “\r” is replaced by a carriage return
  • “\\” is replaced by a backslash
  • “\”” is replaced by a double quote
  • “\'” is replaced by a single quote

// using \n
echo "Hello \nWorld! \n";

// using \t
echo "Hello\tWorld! \n";

// using \$
echo "Hello World! \$ \n";

// using \r
echo "Hello \r World! \n";

// using \\
echo "Hello \\ World! \n";

// using \"
echo "Hello  \"World!\" \n";

// using \'
echo 'Hello  \'World!\' ';
 

Output:


Hello 
World! 
Hello World! 
Hello World! $ 
Hello 
 World! 
Hello \ World! 
Hello  "World!" 
Hello  'World!'

What is a Heredoc syntax String in PHP?

The third method to delimit strings in PHP is the Heredoc syntax (<<<). In Heredoc syntax, an identifier is specified after the heredoc <<< operator, and a new line is then created to write any text. To close the quote, the string is followed by the same identifier which was created at the start. That ending identifier must start on a new line, with no spaces or tabs.

Naming Rules

The heredoc identifier should follow the naming convention that it must contain only alphanumeric characters or underscores, and must start with an underscore or a non-digit character.


$str = <<< welcome
Hai All Welcome to learnetutorials.com
welcome;
echo $str;
 

Output:


Hai All Welcome to learnetutorials.com

In the above example, we can see that we created the string by using the heredoc syntax. It is mostly similar to the double-quoted string.

What is a Newdoc syntax String in PHP?

Newdoc is mostly similar to a heredoc, however, parsing is not performed in newdoc. It's also recognized by three less than symbols <<< followed by an identifier. However, in this case, the identifier is wrapped in a single quote. The same rules apply to newdoc as they do to heredocs.
The main difference between newdoc and heredoc is that the Newdoc is similar to a single-quoted string, while the heredoc is similar to double-quoted string.


$str = <<< 'welcome'
Hai All Welcome to learnetutorials.com
welcome;
echo $str;
 

Output:


Hai All Welcome to learnetutorials.com

In the above example, we can see that we created the string by using the newdoc syntax. It is mostly similar to the single-quoted string.

PHP String Functions

PHP has a various string functions for accessing and manipulating the strings. The most used string functions are mentioned below.

  1. strlen() function: PHP strlen() function is used to return the length of the string.
    
    
    strlen(string);
     
    
    
    $str = 'Welcome to learnetutorials.com ';
    echo strlen($str);
     
    

    Output:

    31
  2. str_replace() function: PHP str_replace () function is used to find and replace the specified word or character from the string.
    
    str_replace(find,replace,string,count);
     
    
    
    $str = 'Welcome to learnetutorials.com ';
    echo $str; //before the function str_replace()
    echo "\n";
    echo str_replace("to", "by", $str); //on using the srt_replace()   
     
    

    Output:

    
    Welcome to learnetutorials.com 
    Welcome by learnetubyrials.com
    
  3. str_split() function: PHP str_split() function is used to split the characters in the string into array.
    
    str_split(string,length);
     
    
    
    
    $str = 'learnetutorials.com';
    print_r(str_split($str));   
     
    

    Output:

    
    Array
    (
        [0] => l
        [1] => e
        [2] => a
        [3] => r
        [4] => n
        [5] => e
        [6] => t
        [7] => u
        [8] => t
        [9] => o
        [10] => r
        [11] => i
        [12] => a
        [13] => l
        [14] => s
        [15] => .
        [16] => c
        [17] => o
        [18] => m
    )
     
  4. strcmp() function: PHP strcmp() function is used to compare two strings and return the difference. 
    
    strcmp(string1,string2);
    
    
    
    $str1 = 'Welcome to learnetutorials.com';
    $str2 = 'Welcome by learnetutorials.com';
    echo strcmp($str1, $str2);
    

    Output:

    
    18
    
  5. strtolower() function: PHP strtolower() function is used to convert the string in to lowercase.
    
    strtolower(string);
    
    
    
    $str = 'Welcome TO learnEtutorials.com';
    echo strtolower($str);
    
    

    Output:

    
    welcome to learnetutorials.com
    
  6. strtoupper() function: The PHP strtoupper () function is used to convert the string into uppercase.
    
    strtoupper(string);
    
    
    
    $str = 'Welcome to learnetutorials.com';
    echo strtoupper($str);
    
    

    Output:

    
    WELCOME TO LEARNETUTORIALS.COM
    
  7. ucfirst() function: PHP ucfirst() function is used to convert the first letter of the string into uppercase.
    
    ucfirst(string);
    
    
    
    $str = 'welcome to learnetutorials.com';
    echo ucfirst($str);
    
    

    Output:

    
    Welcome to learnetutorials.com
    
  8. lcfirst() function: PHP lcfirst() function is used to convert the first letter of the string into lowercase.
    
    lcfirst(string);
    
    
    
    $str = 'WELCOME TO LEARNETUTORIALS.COM';
    echo lcfirst($str);
    
    

    Output:

    
    wELCOME TO LEARNETUTORIALS.COM
    
  9. str_word_count() function: PHP str_word_count() function is used to count the number of words in the string.
    
    str_word_count(string,return(optional),char(optional))
    
    
    
    $str = 'welcome to learnetutorials.com';
    echo str_word_count($str);
    
    

    Output:

    
    4
    
  10. str_repeat() function: PHP str_repeat() function is used to repeat the string for a specified number of times.
    
    str_repeat(string,repeat)
    
    
    
    $str = "welcome to learnetutorials.com \n";
    echo str_repeat($str, 5);
    
    

    Output:

    
    welcome to learnetutorials.com 
    welcome to learnetutorials.com 
    welcome to learnetutorials.com 
    welcome to learnetutorials.com 
    welcome to learnetutorials.com