简体   繁体   中英

Can I echo a variable with single quotes?

Can I echo in single quotes with a variable?

example

echo 'I love my $variable.';

I need to do this because I have lots of HTML to echo too.

You must either use:

echo 'I love my ' . $variable . '.';

This method appends the variable to the string.

Or you could use:

echo "I love my $variable.";

Notice the double quotes used here!

If there's a lot of text, have a look at the Heredoc syntax.

$var = <<<EOT
<div style="">Some 'text' {$variable}</div>
EOT;

The documentation says:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

So the answer is no, not in this way.

The solution is simple:

Don't echo HTML.

As long you are not creating like partial views or stuff , there is no reason to echo HTML. Embed PHP into HTML instead:

<div class="foo">
    <span><?php echo $value; ?></span>
</div>

The benefits are:

  • Don't have to remember escaping of quotes.
  • Easier to maintain HTML code.

†: In this case heredoc [docs] is the least worse alternative.

No. Variables (and special characters) only expand in strings delimited with double quotes. If you want to include a variable in a single-quote delimited string, you have to concatenate it instead:

echo 'I love my '.$variable.'.';

You could also escape the double quotes of your HTML if you'd rather use strings delimited by doble quotes:

echo "<a href=\"$url\">$text</a>";

See the PHP manual on strings , especially the part on string parsing , for more information.

Beside the solutions mentioned by other users such as string concatenation, you can use placeholders as follows;

Note the %s is for string.

$variable = 'what ever'; 
printf('The $variable is %s', $variable);

Try this

<php echo 'I love my '. $variable . ' .'; ?>

No. ' will not parse variables. use

echo 'I love my '.$variable.'.';

or

echo "I love my {$variable}. And I have \" some characters escaped";

not within the string, but you can exit the string and concatenate the variable in.

echo 'I love my '.$variable.'.';

The other option would just be to escape the double quotes.

echo "I love my \"$variable\".";

Or just go in and out of php to echo variables (although ugly).

I love my <?php echo $variable; ?>.
With short tags enabled, I love my <?=$variable?>.

Lastly there is heredoc format that allows both single and double quotes along with variables.

echo <<<HTML
I love my $variable. '"...
HTML;

Taken from php.net: "Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings."

So the answer is no you can't. But you can do it like this:

echo 'I love my' . $variable . '.';

It will be cleaner this way.

use the escape sing-quote to surround the text string as:

variable='my dog'

echo \''I love ' $variable\'

=> 'I love my dog'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM