PHP | Strings & Built-in String functions

Como obter parte de um texto com PHP - Manipulação de Strings - Portal  Visual Dicas

String is a stream or collection of character which is written in quotes. For example: – ‘D’ is taken as Character, and whereas “DevOpsschool” is a String.

In this article, we will discuss about string in details and also it’s in built functions.

Method to create Strings in PHP.

There are mainly two ways to create a String in PHP which is mainly used worldwide.

  1. Single-quote strings

This type of string is written between single-quote (‘’) does not process special characters inside quotes.

For example:-

The above example contain a variable $string1 with String value ‘Welcome to Devopsschool’. Now we print it by using echo method of php and Output is :- Welcome to Devopsschool.

2. Double-quote strings

This type of string is totally similar as we define string, the only difference is, it is written between double-quotes (” “).

We taking the same example as above, but this time we put this string between double-quotes. And the Output is same as above: – Welcome to Devopsschool.

PHP treats everything inside quotes (” “) as Strings. 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.

So, by this example it is clear that we can define a string value in both single and double-quotes as per our requirment and the output has no any changes.
Now, we will take a look on the various string functions and their implementation 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.

Built-in String functions
Built-in functions of string in PHP are some already library functions that can be used directly while coding. Below are some important built-in string functions that we use in our daily and regular programs:

  • strwordcount () function: This function counts total words in a string.
  • strrev () function: This function is used to reverse a string. This function accepts a string as an argument and returns its reversed string. 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.
  • str_replace () function: This function is used to replace any particular character with new one from the String.
  • ucwords () Function: This function is used to transform every starting letter of words to Capital letter.
  • strtoupper() function: This function converts a string into the uppercase string.
  • strtolower() function: This function converts a string into the lowercase string.
  • str_repeat() Function : This function is use to print string for multiple times without writing it again and again.
  • substr() function: This function gives the substring of a given string from a given index.
  • str_split () function: This function converts a string into an array. In this function we can customize the value.

Copy code and apply by itself on your editor for better understanding with the outputs.

I hope you like this particular blog about String and its Functions helpful and informative.

Keep practising and exploring.

Thank You !!!!