PHP- Strings

  • Strings are the set of characters arranged in a sequence.
  • ‘Hello good morning’ is an example of string.
  • Strings are written with in a single quoted or double quoted.
  • Basically single quotes are used to specify simple strings ( ‘ ‘ ).
  • If a variable has to be printed inside a string, it must be double quoted. (” “).

 

  Example 1 [ string using single quotes ]

Output

 

Example 2 [ string using double quotes ]

Output

In example two, The string is initially stored on a variable $st  and then the string is display using echo function. Here we write the variable with in the double quotes for display the string.

 

PHP String functions

For performing operations in php we use some string functions. The main string functions  are described below.

 

strlen()

strlen()  is used to find the length of the string in php. Here the function count not only the character, space also count here. The input string is write inside parenthesis of function.

 

Example 1 [ length of the string ]

Output

 

strrev()

strrev()  is used to reverse the input string.

 

Example 1 [ reverse of  a string ]

Output

 

str_word_count()

str_word_count()  is used to find the number of words present in the given string.

 

Example 1 [ word count of  a string ]

Output

 

strpos()

strpos()  is used to find a specific word or text used in the given string.

 

 Example 1 [ find a particular word from the string ]

Output

 

str_replace()

str_replace()  is used to replace one text with another text with in the same string.

 

Example 1 [ replace text with in the string ]

Output

 

strcmp()

strcmp()  is used to perform comparison of strings.

 

 Example 1 [  comparison of two strings ]

Output

 

Summary
PHP- Strings
Article Name
PHP- Strings
Description
The Strings and String functions are explained in detail.
Author
Publisher Name
Jees K Denny
Publisher Logo

Leave a Reply