-1

I wrote this in 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);
    
?>

I got this error:

Warning: A non-numeric value encountered in C:\xampp\htdocs\myProject\test.php on line 4

Warning: A non-numeric value encountered in C:\xampp\htdocs\myProject\test.php on line 4

this is a line.
this is another line.
0
Community
  • 1
  • 1
Chinmay Ghule
  • 91
  • 1
  • 6
  • 2
    Like in your `echo $s1.'
    '.$s2.` you concatenate using `.`, not `+`
    – brombeer Jan 12 '20 at 15:50
  • 1
    Beyond that, `glueString` is a pointless function. You may as well just say `$foo . $bar` directly in the caller like you're doing and save a lot of typing and confusion about what `glueString` does (it's a big wrapper on `.`, essentially). – ggorlen Jan 12 '20 at 15:52

1 Answers1

1

You need to use . to concatenate a string. + is used to concatenate string in javascript. So you are mixing javascript and PHP here. + is used to add two integer values

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);
Amit Sharma
  • 1,775
  • 3
  • 11
  • 20