PHP String functions

Strings can be seen as a stream of characters. For example, ‘T’ is a character and ‘Tutorialpath’ 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.

 

Output:

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

 

Output:

  • 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, “Tutorialpath” is printed.

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

–> In this article, we will learn about the working of the various PHP 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.

 

Example:

Output:

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.

 

String Functions

 

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:

The PHP String function are part of the PHP core.

  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.

Output:

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

Output:

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

Output:

 

Leave a Reply

Your email address will not be published. Required fields are marked *