Home >Backend Development >PHP Tutorial >Learn PHP(7) php string related applications step by step_PHP tutorial

Learn PHP(7) php string related applications step by step_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:40:35872browse

1. Expression forms of strings
In PHP, strings have three expression forms: single quotes, double quotes, and heredoc.
The PHP manual recommends that under normal circumstances, try to use single-quoted strings. If you need to escape variables, use double-quoted strings. If you need to display multiple lines, use heredoc.
heredoc format:

Copy code The code is as follows:

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

Heredoc has the start symbol and The end character of a piece of text is FOOBAR in this program. However, I personally recommend using a more complex string, so as to avoid errors when encountering the same text in the string.
In addition, one thing to note is that heredoc is not independent of spaces, so the last line terminator must be at the beginning of the line (without spaces and indentation). I have been debugging for a long time just because of this, embarrassing...
But in general applications, heredoc is not very commonly used.
For the difference between single quotes and double quotes, you can refer to my previous article:
2. String output
Regarding the output of strings, there are actually many ways, but I personally think it is useful to only introduce them here. Three of them:
A. echo This is the most commonly used string output form, but here you need to know that echo is not a function, but a language structure. The difference is that functions have return values, but language structures do not. Return value. Of course, this understanding is enough.
B. printf This is often used to combine strings first and then print. In fact, it is very similar to String.Format and then print. If you have learned C language, you will be familiar with this code, so I won’t explain it further:
Copy code 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 advantage of this function is that it can print out some messy things. For example:
Copy code 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);
?>

image
can also be:
Copy code The code is as follows:

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

But this function also has a shortcoming:
Copy code The code is as follows:
print_r(true);
print_r(false);
?>

It turns out that it cannot print normally. Then this function is relatively more suitable for debugging.
D. var_dump, the biggest advantage of this function compared to print_r is that it can print out true and false. The rest of the usage is the same.
3. Common functions of strings
Scripting languages ​​have always been proud of string processing, so I will take a look at the commonly used string processing functions. We can check out PHP's string function library.
Among them, I personally think there are only a few that are commonly used:

Copy code The code is as follows:
$str='HelloPHP';
md5($str); //MD5 encryption
for($i=0;$i{
echo($str($i));
}
strtoupper($str); //Convert to uppercase
strtolower($str); //Convert to lowercase
trim($ str); //Remove the leading blank
rtrim($str); //Remove the right blank
ltrim($str); //Remove the left blank
strrev($str); // String reverse
?>


I feel like there are not many of them, but they all seem useful once I write them down. Forget it, let’s just read the manual.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321377.htmlTechArticle1. Expression forms of strings In PHP, strings have three forms of expression: single quotes, double quotes, and heredoc. The PHP manual recommends that in general, try to use single quote characters...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn