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"