Simple Introduction to PHP string manipulation

Source: Internet
Author: User

In either language, string manipulation is an important foundation, often simple and important. Just as people speak, there is a body (graphical interface), there is a language (print string?). Clearly, the string can explain more things. PHP provides a large number of string manipulation functions, powerful and easy to use. The following will briefly describe its functionality and characteristics.

Weak type

PHP is a weakly typed language, so other types of data can generally be applied directly to string manipulation functions and automatically converted to string types for processing, such as:

echo substr ("1234567", 1, 3);

And

Echo substr (123456,1, 3);

is the same

Defined

A string is typically identified with double or single quotes. Like what

$str = "I love u";

$str = ' I love u ';

There are some differences between the two. The latter treats all single quotes as characters; Like what

$test = "Iwind";

$str = "I love $test";

$str 1 = ' I love $test ';

Echo $str; Will get I love Iwind

echo $str 1; Will get I love $test

The same behavior of the following two examples is not the same:

echo "I love \test"; Will get I love est, which already treats \ t as escaping

Echo ' I love \test '; Will get I love \test

It is possible to simply assume that the contents of the double quotes are "explained" and that single quotes are "WYSIWYG" (particularly, ' will be considered a '). Obviously, the double quotes form is more flexible, of course, single quotes will apply to some special occasions, this is not explained here.

Output

The most common use of output in PHP is echo,print. Both are not real functions, but language constructs, so it is not necessary to invoke a double parenthesis (such as echo ("test");p rint ("Test"). Both can be assigned at the time of output:

echo $str = "Test"; On the one hand output test, on the one hand "test" to the string variable $str

Print $str = "Test";

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.