beardedlinuxgeek
beardedlinuxgeek

Reputation: 1662

Putting a variable between double quotes before echoing?

Now and then I'll see code like

$str = strip_tags($str)
echo "$str";

Does that do anything at all? Why put the variable in quotes instead of echoing it directly?

Upvotes: 3

Views: 245

Answers (2)

deceze
deceze

Reputation: 521995

It's just a terrifically common misunderstanding of new (PHP) programmers. It happens to work exactly the same as not quoting the variable at all, but is entirely superfluous. I'd love to know as well why people think this is necessary; but one can see it surprisingly often.

Upvotes: 3

The Surrican
The Surrican

Reputation: 29856

From the manual:

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

In this case it does not do anything at all.

Strings between double quotes allow the parsing of variables inside, so if you have for example "sometext $variable sometext" the output will be content of the variable with the string sometext before and after it.

If you have only single quotes, you will have the dollar sign and the variable name printed.

So its a short version of concatenating strings and variables, however in this case the string around is is empty, so it has no effect in the sense that you are asking.

There are also other notations. The different behaviour and different types are documented here:

http://php.net/manual/en/language.types.string.php

Upvotes: 3

Related Questions