Detailed explanation of string splicing usage in php

伊谢尔伦
Release: 2023-03-11 15:40:02
Original
26933 people have browsed it

First of all, let me tell you that when learning any language, you must go to the official website to read the documentation, because the official documentation is guaranteed to be correct and the most comprehensive.

There are two string operators.

The first one is the concatenation operator ("."), which returns the string after concatenation of its left and right parameters.

The second one is the connection assignment operator (".="), which appends the right parameter to the left parameter.

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>
Copy after login
rrree

After running, I found that only one '3' was output. Why?

Since the first string "Result3" is created, which is then added to 3 to get 3, non-empty non-numeric strings are converted to 0.

#If you want to output "Result: 6", the code is as follows:

#

<?php
    $var = 3;
    echo "Result:" . $var + 3;
?>
Copy after login

Example below--- If you try to use the concatenation operator with the plus sign, your result will be those numbers as strings

#. ##

<?php
    $var = 3;
    echo "Result:" . ($var + 3);
?>
Copy after login
Braces serve as a good alternative to concatenation, and they make typing faster and the code looks cleaner

Recommended manual

:
PHP is completely self-taught. Manual Remember to use double quotes ("") instead of single quotes ('') as the content is parced by PHP, because in single quotes ('') you will get the provided variable litaral name
<?php

echo "thr"."ee";           //prints the string "three"
echo "twe" . "lve";        //prints the string "twelve"
echo 1 . 2;                //prints the string "12"
echo 1.2;                  //prints the number 1.2
echo 1+2;                  //prints the number 3

?>
Copy after login
<?php

$a = &#39;12345&#39;;

// This works:
echo "qwe{$a}rty"; // qwe12345rty, using braces
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used

// Does not work:
echo &#39;qwe{$a}rty&#39;; // qwe{$a}rty, single quotes are not parsed
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined

?>
Copy after login
It can be seen that using ('') means the content in single quotes is echoed directly, while using ("") retains the variable.

# Recommended related articles:


1

.
What is the string connection operator in php
2.Comparison of the efficiency of several string connections in php
Related video recommendations: 1.Dugu Jiujian (4)_PHP video tutorial

The above is the detailed content of Detailed explanation of string splicing usage in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template