PHP String Variables

Submitted by: 
Language: 
Visitors have accessed this post 8694 times.

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:

  1. <?php
  2. $txt = "Welcome to my website, ";
  3. $name = "Grace";
  4. echo $txt . " " . $name;
  5. ?>

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:

  1. <?php
  2. echo strlen("Welcome to my website ");
  3. ?>

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:

  1. <?php
  2. echo strpos("Welcome to my website, Grace","website");
  3. ?>

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

i am really interested in your helpful tutorials

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.