Step by step learn about PHP of 7 PHP string related applications

  • 2020-03-31 20:22:43
  • OfStack

1. The representation of a string
In PHP, strings take three forms: single, double, and heredoc.
The PHP manual recommends using single-quoted strings in general. Use a string with double quotes if you need to escape a variable. Use heredoc if multiple lines are required.
Heredoc form:
 
<?php 
$test=<<< FOOBAR 
Hello World! 
Hello PHP! 
FOOBAR; 
echo $test; 
?> 

Heredoc is a piece of text with a beginning and an end, in this case FOOBAR, but my personal recommendation is to use a more complex string to avoid errors in the string when you encounter the same text.
Also, it is important to note that in heredoc it is not whitespace independent, so the last line terminator must be at the beginning of the line (no Spaces and indentation). I have been debugging this for a long time. .
However, heredoc is not commonly used in general applications.
For the difference between single and double quotes, see my previous post:
2. String output
There are many ways to output strings, but here are the three I think are useful:
A. Echo is the most common form of string output, but here you need to know that echo is not A function, but A language structure. The difference is that functions return values, but language structure does not return values. Of course, that's all you need to know.
B. Printf is often used for String combination before printing, which is actually similar to string.format and then printing.
 
<?php 
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.
 
<?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); 
?> 

(link: https://www.jb51.net/upload/2010-3/20100305174752485.png)  
It could also be:
 
<?php 
$arr=array('1'=>'kym','2'=>'sina','3'=>'blog'); 
print_r($arr); 
?> 

But this function also has a drawback:
 
<?php 
print_r(true); 
print_r(false); 
?> 

It turned out to be unable to print properly. Then this function is relatively good for debugging.
D. var_dump, the biggest advantage of this function over print_r is that it prints true and false. The rest of the usage is consistent.
3. Common functions of strings
Scripting languages pride themselves on string manipulation, so I'll take a look at some common string manipulation functions. Take a look at PHP's string library.
Among them, I think only a few are commonly used:
 
<?php 
$str='HelloPHP'; 
md5($str); //MD5 encryption
for($i=0;$i<strlen($str);$i++) 
{ 
echo($str($i)); 
} 
strtoupper($str); //To uppercase
strtolower($str); //Converted to lowercase
trim($str); //Remove the space in the first place
rtrim($str); //Get rid of the white space on the right
ltrim($str); //Get rid of the white space on the left
strrev($str); //String inversion
?> 

Feel a few, a write feel all very useful, calculate, still read manual.

Related articles: