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:
<? PHP
$ Test=<Foobar
Hello world!
Hello PHP!
Foobar;
Echo$ Test;
?>
Heredoc is a piece of text with the start and end characters.ProgramIt is foobar, but it is recommended that you use a more complex string to avoid errors in the same text in the string.
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 previousArticle:
2. String output
There are actually many ways to output strings, but here I personally think that only three of them are useful:
A. EchoThis is the most common form of string output, 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. printfThis is often used for string combination and printing. In fact, it is very similar to string. Format and then printing. If you have learned the C language, you will certainlyCodeIf you are familiar with it, I will not explain it more:
<? PHP
Printf ('Hello % s, I am % d','World',22);
?>
C. print_rThis function is mainly used for debugging. The biggest benefit of this function is that it can print out some messy things. For example:
<? PHP
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:
<? PHP
$ Arr=Array('1'=>'Kym','2'=>'Sina','3'=>'Blog');
Print_r ($ Arr);
?>
However, this function also has the following Disadvantages:
<? PHP
Print_r (True);
Print_r (False);
?>
The data cannot be printed normally. Then, this function is more suitable for debugging.
D. var_dumpThe 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:
<? PHP
$ Str ='Hellophp' ;
MD5 ( $ Str ); // MD5 Encryption
For ( $ I = 0 ; $ I <Strlen ( $ Str ); $ I ++)
{
Echo ( $ Str ( $ I ));
}
Strtoupper ( $ Str ); // Convert to uppercase
Strtolower ( $ Str ); // Convert to lowercase
Trim ( $ Str ); // Remove the leading Blank
Rtrim ( $ Str ); // Remove the white space on the right
Ltrim ( $ Str ); // Remove the left blank
Strrev ( $ Str ); // String Inversion
?>
I don't think there are a few. I think it is very useful to write it. Forget it. Read the manual.