开发者

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.


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.


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 . ' .'; ?>


In my case, I have changed my command as follows and it worked for me:Added double quotes twice("") beside every single quote(')

PVALUE='1,2,3'
echo "variable='""${PVALUE},repmgr'""" >>postgresql.conf
  • Output:

    variable='1,2,3,repmgr'

#How to print a variable in echo within a single quote bash shell script


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

variable='my dog'

echo \''I love ' $variable\'

=> 'I love my dog'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜