From 5af5cbb837cb1fc0ad9ba37d5a82b690010dedf3 Mon Sep 17 00:00:00 2001 From: NathanFreeman <1056159381@qq.com> Date: Mon, 13 Jul 2026 22:26:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=8B=BC?= =?UTF-8?q?=E6=8E=A5=E4=B8=AD=EF=BC=8C=E9=99=A4=E4=BA=86=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=85=83=E7=B4=A0=E4=B9=8B=E5=A4=96=EF=BC=8C=E5=89=A9?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=A6=82=E6=9E=9C?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E7=A9=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E8=BF=99=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Parser/BinaryOpTrait.php | 15 +++++++++++---- tests/compiler/concat_empty.phpt | 12 ++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tests/compiler/concat_empty.phpt diff --git a/src/Parser/BinaryOpTrait.php b/src/Parser/BinaryOpTrait.php index e57f987c..8d4d32b1 100644 --- a/src/Parser/BinaryOpTrait.php +++ b/src/Parser/BinaryOpTrait.php @@ -264,10 +264,17 @@ trait BinaryOpTrait $this->flattenConcatExpr($expr, $items); $argList = $prefixExpressions; - foreach ($items as $item) { - $type = $this->detectTypeOfExpr($item); - $parsed = $this->parseExprAsValue($item); - $argList[] = $this->prepareConcatOperand($parsed, $type); + foreach ($items as $index => $item) { + // 在字符串拼接中,除了第一个元素之外,剩下的字符串如果出现空字符串,忽略这个字符串 + if ($index > 0 && $item->value == '') { + continue; + } + + $argList[] = $this->prepareConcatOperand($this->parseExprAsValue($item), $this->detectTypeOfExpr($item)); + } + + if (sizeof($argList) == 1) { + return $argList[0]; } return Symbol::concat() . '({' . implode(', ', $argList) . '})'; diff --git a/tests/compiler/concat_empty.phpt b/tests/compiler/concat_empty.phpt new file mode 100644 index 00000000..d759d8e1 --- /dev/null +++ b/tests/compiler/concat_empty.phpt @@ -0,0 +1,12 @@ +--TEST-- +Concat empty strings +--FILE-- + +--EXPECT-- +string(2) "1a" +string(1) "1"