PHP String

 The application of PHP String is quite common. When we create a string, it can be used in a variable or function, and the String can be output through PHP echo at any time . Besides, PHP String can be used to simply display the string. In addition, if you set it to a number, you can also perform calculations, but in the programming code of the PHP script, please pay attention to whether there is support for the content of the string to avoid the occurrence of garbled characters, or use UTF-8 as the main encoding rule. Here are some examples of strings.


PHP String example
<?php
 $str='Today's weather is really good';
 echo $str;
?>
The above output result: Today’s weather is really good. In the

example, single quotes are used on the right side of the equal sign of $str. In fact, according to the function of PHP echo , double quotes or parentheses can also be used. This example Just simply output the text String on the screen, let's take a look at a simple example of stringing two Strings together.

PHP String concatenation operator
<?php
 $str1='Today's weather is really good';
 $str2=', very suitable for sports';
 $str3=', go to play ball after work';
 echo $str1 . $str2 . $str3;
?>
The output result of the above example: today’s weather is really good, it’s very suitable for sports, and go to play ball after work.

In this example, there are three Strings. We use the Concatenation Operator, which is a half-shaped point, such as the red mark in the example. (The point is very small, you may need to look carefully), string together three Strings. This operator is currently the only string concatenation operator in PHP. It has no calculation function and can only concatenate Strings. Of course, PHP String also You can use other operators to do mathematical calculations, please see the following example.

PHP String Numerical Operations
<?php
 $str1='1';
 $str2='2';
 echo $str1+$str2;
?>
The output of the above example: 3

should not be difficult to understand. In the example, "echo $str1+$str2" means to add the numbers of the two string variables before outputting to the screen . In addition to adding, it can also be For other operations, such as multiplication, division, subtraction... etc., the part after echo can be wrapped in double quotes, but single quotes cannot be used, otherwise it will output a result like "$str1-$str2".

Post a Comment

0 Comments