【问题标题】:Cannot append string in a function [duplicate]无法在函数中附加字符串[重复]
【发布时间】:2020-04-29 12:13:01
【问题描述】:

我是用 PHP 写的:

<?php   
    function glueString($str1, $str2)   {
            $tempstr = $str1 + $str2;
            return $tempstr;
    }   
    $s1 = 'this is a line.';
    $s2 = 'this is another line.';
    echo $s1.'<br/>'.$s2.'<br/>'.glueString($s1, $s2);
    
?>

我收到了这个错误:

警告:在第 4 行的 C:\xampp\htdocs\myProject\test.php 中遇到非数字值

警告:在第 4 行的 C:\xampp\htdocs\myProject\test.php 中遇到非数字值

this is a line.
this is another line.
0

【问题讨论】:

  • 就像在你的echo $s1.'&lt;br/&gt;'.$s2. 中你使用. 连接,而不是+
  • 除此之外,glueString 是一个毫无意义的函数。你也可以像你正在做的那样直接在调用者中说$foo . $bar,这样就可以省去很多关于glueString做什么的打字和困惑(本质上,它是.的一个大包装)。

标签: php append


【解决方案1】:

您需要使用. 来连接字符串。 + 用于在 javascript 中连接字符串。所以你在这里混合了javascript和PHP。 + 用于将两个整数值相加

function glueString($str1, $str2)   {
    $tempstr = $str1.' '.$str2;
    return $tempstr;
}

$s1 = 'this is a line.';
$s2 = 'this is another line.';
echo $s1.'<br/>'.$s2.'<br/>'.glueString($s1, $s2);

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2013-12-17
    • 2016-12-22
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多