Step by step to learn PHP (7) php string related app _ PHP Tutorial

Source: Internet
Author: User
Learn PHP (7) php string-related applications step by step. 1. the character string is represented in PHP. the character string has three forms: single quotation marks, double quotation marks, and heredoc. The PHP Manual recommends that you use single quotes whenever possible. 1. string representation
In PHP, a string has three forms: single quotes, double quotation marks, and heredoc.
The PHP Manual recommends that you use single quotes strings whenever possible. To escape a variable, use a double quotation mark string. Heredoc is used only when multiple lines are displayed.
Heredoc format:

The code is as follows:


$ Test = <FOOBAR
Hello World!
Hello PHP!
FOOBAR;
Echo $ test;
?>


Heredoc is a piece of text with the start and end characters. it is FOOBAR in this program. However, it is recommended that you use more complex strings, in this way, you can avoid errors in strings with the same text.
In addition, it should be noted that in heredoc, it is not space-independent, so the last line must start with the line (no space or indentation ). This is why I have been debugging for a long time .......
However, heredoc is not commonly used in general applications.
For the difference between single quotes and double quotes, refer to my previous article:
2. string output
There are actually many ways to output strings, but here I personally think that only three of them are useful:
A. echo is the most commonly used string output form, but here we need to know that echo is not a function, but a language structure. The difference is that functions have returned values, but the language structure does not return values. Of course, you can understand this.
B. printf is usually used to combine strings and print them. in fact, it is similar to String. Format and then print them. if you have learned the C language, you will be familiar with this code and will not explain it more:

The code is as follows:


Printf ('HELLO % s, I am % d', 'World', 22 );
?>


C. print_r. This function is mainly used for debugging. The biggest benefit of this function is that it can print out some messy things. for example:

The code is as follows:


Class People
{
Private $ name;
Public function People ($ name)
{
$ This-> name = $ name;
}
Public function Say ()
{
Echo ('Hello'. $ name );
}
}
$ P = new People ("kym ");
Print_r ($ p );
?>



It can also be:

The code is as follows:


$ Arr = array ('1' => 'kym', '2' => 'sina ', '3' => 'blog ');
Print_r ($ arr );
?>


However, this function also has the following disadvantages:

The code is as follows:


Print_r (true );
Print_r (false );
?>


The data cannot be printed normally. Then, this function is more suitable for debugging.
D. var_dump. The biggest advantage of this function over print_r is that it can print true and false. The rest are used in the same way.
3. common functions of strings
The scripting language is always proud of string processing, so let's take a look at the commonly used string processing functions. Let's take a look at the PHP string function library.
Specifically, I think there are only a few commonly used:

The code is as follows:


$ Str = 'hellophp ';
Md5 ($ str); // MD5 encryption
For ($ I = 0; $ I {
Echo ($ str ($ I ));
}
Strtoupper ($ str); // converts it to uppercase
Strtolower ($ str); // converts it to lowercase.
Trim ($ str); // removes the leading blank
Rtrim ($ str); // remove the white space on the right
Ltrim ($ str); // remove the left blank
Strrev ($ str); // reverse string
?>


I don't think there are a few. I think it is very useful to write it. forget it. Read the manual.

Http://www.bkjia.com/PHPjc/321377.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321377.htmlTechArticle1. the representation of the string in PHP, the string has three forms: single quotes, double quotes, and heredoc. The PHP Manual recommends that you use single quotes as much as possible...

Related Article

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.