PHP string addition



 PHP string addition is a basic technique for string processing. Common PHP string addition can be divided into simple string concatenation and numeric string calculation. The concatenation of strings uses concatenation operators. It is the half-shape dot (.). The addition of numerical calculation strings uses the plus sign (+) of the mathematical operator. Let's take a look at the basic syntax of these two PHP string additions, and then apply the examples The actual operation is for your reference.


The basic syntax of PHP string addition
Addition of text string: string. Addition of string
number: number + number
From the basic grammar of string addition, we can find that the symbols used for adding text and numbers are different, and the result will be different. After the text is added, it will become a longer string, and the content is the original string, but Connected together, the number will become a new number after adding.

A practical example of PHP string addition
<?php
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; //Web page encoding

$stringA='25';
$stringB='77'; echo'Add

text string:';
echo $stringA.$stringB ;
echo'<br> Add numeric string:';
echo stringA +$ stringB ;
?>
Example addition result
Add text string: 2577 Add
digital string: 102
At the beginning of the example, the echo string is only an announcement of the web page encoding, not the key point. The next is the key point. In the example, we have prepared two strings, both of which are numbers. We output the sum of text strings through echo . It is concatenation, and the addition of numeric strings, that is, mathematical operations, such as the part marked in red in the example, the result of the addition can be seen, the result of the addition with the string concatenation operator (.) becomes four A string of numbers, and the result of the addition with the mathematical plus operator becomes 102.

Note: In PHP, there is actually no special distinction between numbers and text strings, that is, both numbers and text are called strings. The only difference is that numeric strings can be used for calculations. If you are interested in PHP If you are interested in string rules, please refer to the introduction in PHP String Strings ".

Post a Comment

0 Comments