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:
- <?php
- $txt = "Welcome to my website, ";
- $name = "Grace";
- echo $txt . " " . $name;
- ?>
The above code will output the following string:
The strlen() function is used to count the number of character in a string variable.
Example:
- <?php
- ?>
The output will be:
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:
- <?php
- ?>
The output will be:
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
- Add new comment
- 246 views