Skip to main content

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.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.