8. PHP Tutorial _php Strings

Source: Internet
Author: User

String variables are used to store and manipulate text.

String Variables in PHP

String variables are used to contain values that have characters.

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

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

1 <? PHP 2     $txt = "Hello world!" ; 3     Echo $txt ; 4 ?>

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

Now, let's look at some of the functions and operators used to manipulate strings.

PHP collocated operator

In PHP, there is only one string operator.

The collocated operator (.) Used to connect two string values together.

The following example shows how to concatenate two string variables together:

1 <? PHP 2     $txt 1 = "Hello world!" ; 3     $txt 2 = "What a nice day!" ; 4     Echo $txt 1. "". $txt 2 ; 5 ?>

The above code will output: Hello world! What a nice day!
Tip: In the above code, we have used the two-time collocated operator. 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 length (number of characters) of the string.

The following instance returns the string "Hello world!" The length of:

1 <? PHP2     strlen ("Hello world!" ); 3?>    

The above code will output 12

Tip: 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 find a character or a specified text within a string.

If a match is found in the string, the function returns the first matching character position. Returns False if no match is found.

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

1 <? PHP 2     Echo Strpos ("Hello world!", "World"); 3 ?>

The above code will output: 6
Tip: In the above example, the string "World" is positioned at 6. The reason for 6 instead of 7 is that the position of the first character in the string is 0, not 1.

8. PHP Tutorial _php Strings

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.