The third step of the php basic tutorial learns strings and related functions

String

In the previous section, we learned some concepts of php variables, constants and data types. In this section, we will learn more about the string type and related operation methods in the data type.

String refers to a collection of characters. For example, a single character a, a single character b, a single character c...These a, b, and c are all characters; when these characters are connected and combined into abc, it is a character string.

In the PHP language, the way to create a variable is $a, and the variable assignment string can be written as $a="abc";. In php programming, the distinguishing feature of strings is that they have double quotes or single quotes. For example, the values ​​"abc", "hello", "a", and'a' are all strings.

The difference between single quotes and double quotes

Perhaps after reading the above introduction, some beginners may wonder "What is the difference between single quotes and double quotes?".

The main difference between single quotation marks and double quotation marks is whether variables can be added to the string, and the string content of the variable is parsed during execution. Mainly check the following code:

<?php
	$txt='Li Hua ';
	echo $txt;
?>

Code definition
$txt='Li Hua ';: define a string variable txt, whose value is Li Hua and
then output the content of $txt.
Turn on the service and use the browser to access the result as follows:
Insert picture description here
The string of Li Hua is output directly from the running result. Then we look at another example:

<?php
	$txt='Li Hua ';
	$txt_="Hello $txt";
	echo $txt_;
?>

$txt='Li Hua ';: Define the txt variable, the value is Li Hua
$txt_="Hello $txt";: A $txt_ variable, the value is Hello $txt The
main check Hello $txtin the above code , the string is added a string $txt, and the outermost layer uses double quotes. Double quotes will parse the variable tags inside, here is the $txt variable. When outputting, the content of the string $txt will be replaced with Li Hua for output.
The results are as follows:

Insert picture description here
Now the $txt_="Hello $txt";double quotes in single quotes to try to change.

<?php
	$txt='Li Hua ';
	$txt_='Hello $txt';
	echo $txt_;
?>

The results are as follows:
Insert picture description here

Connection between strings

In order to facilitate the explanation, we continue to use the above example to modify. code show as below:

<?php
	$txt='Li Hua ';
	$txt_='Hello ';
	echo $txt_.$txt;
?>

The above code defines two variables, one is txt and the other is txt_, similar to the previous one. Then use echo to output. But it should be noted that when using echo, a decimal point is used between the variable $txt and the variable $txt_ to connect. During output, these two variables will be spliced ​​during output.
The results are as follows:
Insert picture description here

The length of the string

String variables have length values. For example, "abcdefg" has 7 characters, so its length is 7. In PHP code, you can use the strlen function to get the string or the length of the string variable. Examples are as follows:

<?php
	$txt='Li Hua ';
	$txt_='Hello ';
	echo strlen($txt_);
?>

The results are as follows:
Insert picture description here
or:

<?php
	$txt='Li Hua ';
	$txt_='Hello ';
	echo strlen('Hello ');
?>

Find the position of the specified character in the string

You can use the strpos function to find the position of a specified character in a string. The following code:

<?php
	$txt='Li Hua ';
	$txt_='Hello';
	echo strpos($txt,'i');
	echo '-';
	echo strpos($txt_,'e');
?>

The result is as follows: The
Insert picture description here
strpos function is used to find the position of the string. The strpos function can pass in 2 parameters. First, you have to specify which string you want to find, and then tell this function what value you need to find, so The above example is written as follows:

echo strpos($txt,'i');

The definition is to find the position of the character i in the $txt variable. Of course, the search content is more than one character i, but also continuous characters, which is the position of the string.

String replacement

String replacement can use the str_replace function to specify what you want to replace, what new value you want to replace, and which string to replace in. After giving str_replace this information, str_replace will return the value after the string is replaced. The following code:

<?php
	$txt='Li Hua ';
	echo str_replace("Hua","Ming",$txt);
?>

The above original string variable is $txt value Li Hua, use str_replace function, the first position specifies the content to be replaced, the second is the new content after replacement, and the third is the original string or string Variables, these specified parameters are separated by commas (must be English punctuation).
The results are as follows: The
Insert picture description here
php series of tutorials are being continuously updated.

Guess you like

Origin blog.csdn.net/A757291228/article/details/107249773
Recommended