Can I repeat a variable with single quotes?

Is it possible to echo in single quotes with a variable?

Example

echo 'I love my $variable.';

I need to do this because I have a lot of HTML for echo.

+7
source share
11 answers

You should either use:

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

This method adds a variable to the string.

Or you could use:

echo "I love my $variable.";

Check out the double quotes used here!

+21
source

If there is a lot of text, look at the Heredoc syntax.

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

:

: heredoc, variables escape- .

, , .

:

echo HTML.

, , HTML. PHP HTML:

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

:

  • .
  • HTML-.

†: heredoc [docs] .

+3

. ( ) , . , :

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

HTML, , doble:

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

. PHP , , .

+2

. ' .

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

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

, .

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

, .

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

php echo ( ).

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

, heredoc, , .

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

php.net: ". heredoc, escape- , ."

, - , . :

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

.

0

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

Note that% s is for the string.

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

Try it

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

use the escape escape quote to surround the text string as follows:

variable='my dog'

echo \''I love ' $variable\'

=> "I love my dog"

-1
source

Or a simple way:

awk "\$1 < $variable"
-5
source

All Articles