Step-by-Step learning PHP (7) PHP string-related applications _php tutorial

Source: Internet
Author: User
Tags md5 encryption
1. The representation of a string
In PHP, there are three representations of strings: single quotes, double quotes, and Heredoc.
The PHP manual suggests that, in general, use single-quote strings as much as possible. If you need to escape a variable, use a double-quoted string. Use Heredoc If multiple lines of display are required.
Heredoc form:
Copy CodeThe code is as follows:
$test =<<< FOOBAR
Hello world!
Hello php!
FOOBAR;
Echo $test;
?>

Heredoc is a piece of text with a start character and a Terminator, which is foobar in this procedure, but it is advisable to use a more complex string so that you avoid encountering errors with the same text in the string.
In addition, it is important to note that in Heredoc is not a space-independent, so the last line end must be at the beginning of the line (no space and indentation). I just because of this debugging n long, embarrassed ....
But in general applications, Heredoc are not very common.
For the difference between single and double quotes, you can refer to my previous article:
2. Output of the string
There are many ways to output a string, but here's a personal thought to introduce only three useful:
A. Echo this is the output form of the most commonly used string, but here you need to know that echo is not a function but a language structure, except that the function has a return value, but the language structure has no return value. Of course, this understanding is possible.
B. printf This is often used for strings before printing, in fact, very similar to String.Format and then print, if you learn C language, will be familiar with this code, do not explain more:
Copy CodeThe code is as follows:
printf (' Hello%s,i am%d ', ' world ', 22);
?>

C. Print_r, this function is mainly used for debugging, the biggest advantage of this function is to be able to print out some messy things, for example:
Copy CodeThe 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:
Copy CodeThe code is as follows:
$arr =array (' 1 ' = ' Kym ', ' 2 ' = ' sina ', ' 3 ' = ' blog ');
Print_r ($arr);
?>

But this function also has a drawback:
Copy CodeThe code is as follows:
Print_r (TRUE);
Print_r (FALSE);
?>

The result is that it cannot be printed properly. Then this function is more appropriate for debugging.
D. Var_dump, the biggest advantage of this function compared to print_r is that it can print true and false. The rest of the usage is consistent.
3. Common functions for strings
Scripting languages are always proud of string processing, so I'll look at the usual string handling functions. We can look at Php's String function library.
Among them, individuals think that only a few of the usual:
Copy CodeThe code is as follows:
$str = ' hellophp ';
MD5 ($STR); MD5 encryption
for ($i =0; $i {
Echo ($str ($i));
}
Strtoupper ($STR);//Turn to uppercase
Strtolower ($STR),//Turn lowercase
Trim ($str),//Remove the first blank
RTrim ($STR);//Remove Right Blank
LTrim ($STR);//Remove left blank
Strrev ($STR);//String inversion
?>

Feel a few, write up feel very useful, forget it, or read the manual.

http://www.bkjia.com/phpjc/321377.html www.bkjia.com true http://www.bkjia.com/phpjc/321377.html techarticle 1. The representation of a string in PHP, there are three representations of strings: single quotes, double quotes, and Heredoc. The PHP manual suggests that, in general, use single-quote characters 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.