0

I’m learning to program in PHP and I learned that I can print variables with text through different methods but I have a question:

What is the difference between echo $var1 . ' ' . $var2 and echo "{$var1} {$var2}" in PHP? And what to use?

I don’t understand why do we have use curly brackets and even if it is another method what is the difference between the two.

Thank You!

Arch
  • 109
  • 11
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Phiter Jul 30 '18 at 14:27
  • One is called concatenation and the other is called the complex (curly) syntax. – Daan Jul 30 '18 at 14:28
  • No, I know the difference between double quotes and single quotes I just don’t understand why do I need to put curly brackets – Arch Jul 30 '18 at 14:29
  • But what’s the difference between the complex syntax and the concatenation? – Arch Jul 30 '18 at 14:30
  • 1
    For those variables you don't need the curly braces, it'll work just fine without them. They're useful only when you're displaying a more "complex" variable, like `$foo->property['something']`. See https://3v4l.org/bRtiU – iainn Jul 30 '18 at 14:31
  • Can you post it as an answer? – Arch Jul 30 '18 at 14:37

3 Answers3

1

Per your comment:

No, I know the difference between double quotes and single quotes I just don’t understand why do I need to put curly brackets

You don't "need" curly brackets but they can help in reading your interpolated string.

Please do not do this in your code but consider:

<?php
$hi = 'h';
$hii = 'sh';

echo "$hiie";

Do you get hie or she as the result?

Answer: neither, you get

Notice: Undefined variable: hiie

To fix this then you need to be explicit with one of the following lines:

echo "{$hi}ie"; // hie
echo "{$hii}e"; // she
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
1

The first method is the concatenation method and is straightforward. The second method: you can print variables inside double quotation marks like echo " $var1 $var2 "; but what if you want to print something directly b/w, before, or after a variable. e.g: echo " $var1SomeOtherTextNotFromVariable$var2SomeMoreText "; PHP will assume that all text as variable until another $ is reached. To solve this you need to tell PHP where does variable start and end. e.g: echo " {$var1}SomeOtherTextNotFromVariable{$var2}SomeMoreText ";

Note Please check Progrock's answer for another benefit of using curly braces.

Noor Ahmed
  • 178
  • 12
1

(Not an answer but worthy of consideration.)

Curlies are useful if you are dealing with arrays:

$foo =
[
    'name' => 'Foo',
    'age'  => 23
];

echo "Name: {$foo['name']}, Age: {$foo['age']}\n";

Output:

Name: Foo, Age: 23

If you omit the curlies you get an error like:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in ...
Progrock
  • 7,373
  • 1
  • 19
  • 25