PHP string variable

Source: Internet
Author: User
String variables are used to store and process text. String variables are used to store and process text.

String variables in PHP

String variables are used to contain character values.

After creating a string, we can operate it. You can use a string directly in a function or store it in a variable.

In the following example, we create a string variable named txt and assign it to "Hello world! ". Then we output the txt variable value:

Instance

 


Note: When you assign a text value to a variable, remember to add single quotation marks or double quotation marks to the text value.

Now, let's take a look at some common operators and operators that operate strings.

PHP concatenation operator

In PHP, there is only one string operator.

The concatenation operator (.) is used to connect two string values.

The following example shows how to connect two string variables:

Instance

 

The above code will output: Hello world! What a nice day!

Tip: In the above code, we have used the concatenation operator twice. This is because we need to insert a space between two strings.

PHP strlen () function

Sometimes it is useful to know the length of a string value.

The strlen () function returns the string length (number of characters ).

The following instance returns the string "Hello world! "Length:

Instance

 

The above code will be output: 12

Note: strlen () is often used in loops and other functions, because it is important to determine when the string ends. (For example, in a loop, we need to end the loop after the last character in the string .)

PHP strpos () function

The strpos () function is used to search for a character or a specified piece of text in a string.

If a match is found in the string, the function returns the first matched character position. If no matching is found, FALSE is returned.

The following instance is in the string "Hello world! "Search text" world ":

Instance

 

The above code will be output: 6

Tip: In the above instance, the position of the string "world" is 6. The reason is 6 rather than 7 is that the first character in the string is 0 rather than 1.

The above is the content of the PHP string variable. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.