-1

I am new to Laravel and I am having this question. I tried out this line of code and it works fine: return redirect("/cards/{$note->id}");

But when ever I try to use the single quotes, it does not work: return redirect('/cards/{$note->id}');

How can I solve this problem ?

Mladen Janjetovic
  • 13,844
  • 8
  • 72
  • 82
Prince
  • 1,190
  • 3
  • 13
  • 25

3 Answers3

3

What you are doing first is called variable interpolation or string interpolation. You can read more about it here, on PHP docs and here, on Wiki.

It's a feature in PHP that allows you to pass a string and have variables/placeholders inside interpreted.

In your second example you are using single quotes, which does not provide this feature, so you will have to break it up and add the variable manually to the string:

return redirect('/cards/' . $note->id);

If you are interested in a more elaborate explanation and the performance behind it then you can read more on this answer here by Blizz

He concludes that:

Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.

However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.

Community
  • 1
  • 1
Nicklas Kevin Frank
  • 6,079
  • 4
  • 38
  • 63
2

You should use "/cards/{$note->id}" or '/cards/'.$note->id

The most important feature of double-quoted strings is the fact that variable names will be expanded.

When a string is specified in double quotes or with heredoc, variables are parsed within it.

From PHP documentation

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • The curly braces are part of the string interpolation, and should not be included in the single-quoted string at all. – Moshe Katz Sep 27 '16 at 12:14
0

Use it like that:

return redirect('/cards/'. $note->id);

With either single or double quotes

Marcin
  • 1,488
  • 1
  • 13
  • 27