PHP String Variables

A string variable is used to manipulate any character stored in a variable you declared.

This chapter will discuss common function you can use to manipulate string variables.

Some common functions are:

  • The Concatenation Operator
  • The strlen() function
  • The strpos() function

The concatenation operator is very useful to combine two string values.

Example:

The above code will output the following string:

Welcome to my website, Grace

The strlen() function is used to count the number of character in a string variable.

Example:

The output will be:

22

Strlen() function is useful if you want to limit the number of character in a string. Example is if you want to display only 60 characters as an excerpt in your post at the front page of your site.

The strpos() function is used to search a string in a sentence or paragraph.

Example:

The output will be:

14

The above code simply search for the word “website” in a sentence “Welcome to my website, Grace”.

If none is found, then it will return false.

There are so many useful functions to manipulate string in PHP. To know more about it please visit PHP String Reference.

Comments

Add new comment