PHP Quick Start Learning-6 (constant)

Source: Internet
Author: User

PhpString Variables

String variables are used to store and manipulate text.

String variables in PHP

String variables are used to contain values that have characters.

After the string has been created, we can manipulate 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 a value of "Hello world!". Then we output the value of the TXT variable:

<?  $txt= "Hello world!"  echo$txt?>

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

PHP collocated operator

In PHP, there is only one string operator.

The collocated operator (.) is used to concatenate two string values together.

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

<?  $txt 1= "Hello world!"  $txt 2= "What a nice day!"  echo$txt 1$txt 2?>

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

hint: in the above code, we have used two times the collocated operator. This is because we need to insert a space between the 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 length of the string "Hello world!":

<?  echostrlen("Hello world!"  ?>

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. If no match is found, FALSE is returned.

The following example finds the text "World" in the string "Hello world!":

<?  echostrpos("Hello world!", "World"?>

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 a string is 0, not 1.

Note:

<? PHP Echo strlen ("Chinese characters");   // outputis?>12, because a Chinese is 3 characters in number. You can use the Mb_strlen setting to specify the number of characters in the encoding output:< PHP Echo Mb_strlen ("Chinese Characters", ' Utf-8 ');  // Output 4?>

PHP Quick Start Learning-6 (constant)

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.