-3

I'm trying to merge some integers and strings together and output them with echo.

What I've done:

$a=1
$b=2
$c=3
$d=4

echo 'var saveName = "' . $a . '_KID=' . $b . '_RID=' . $c . '";var RID = ' . $d . ';';

however that throws an error:

Parse error: syntax error, unexpected '$b' (T_VARIABLE) in ...

I don't see whats wrong here. What causes that error?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80

3 Answers3

3

You are missing semicolons:

$a=1;
$b=2;
$c=3;
$d=4;
Datsheep
  • 382
  • 1
  • 4
  • 16
1

It's a matter of missing semicolons in your variable declarations, not invalid string merging. Have in mind that php does require programmer to end instructions with semicolons.

0

In your code, your are missing the semicolons at the end of every instructions.

$a = 1;
$b = 2;
$c = 3;
$d = 4;

echo "var saveName = \"{$a}_KID={$b}_RID={$c}\";var RID = {$d};";

You should use a proper editor that lints the code, or even something like 3v4l that show your errors.

Also, you can use string interpolation with echo, that is slightly faster than concatenation, especially in PHP7, and also easier to read.

Thomas Dutrion
  • 1,844
  • 1
  • 11
  • 9