PHP echo variable and html together

I know this is the main php question, but I am having problems displaying variables inside the HTML echo. Below I am trying to achieve.

<?php
    $variable = 'testing';
    echo <span class="label-[variable]">[variable]</span>
?>

should be displayed:

<span class="label-testing">testing</span>
+5
source share
10 answers
<?php
    $variable = 'testing';
    echo <span class="label-[variable]">[variable]</span>
?>

Must be

<?php
    $variable = 'testing';
    echo '<span class="label-' . $variable .'">' . $variable . '</span>';
?>
+7
source

If your HTML contains double quotes, you can wrap it in single quotes:

echo '<span class="label-' . $variable . '">' . $variable . '</span>';

Alternatively, you can do this inline by avoiding double quotes:

echo "<span class=\"label-$variable\">$variable</span>";

The use of double quotes also allows for special characters, such as \nand \t.

The documentation is your friend.

+2
source

PHP- HTML - heredoc:

<?php
    $variable = 'testing';
echo <<<TEXT
    <span class="label-{$variable}">{$variable}</span> 
TEXT;
?>

<<<TEXT and TEXT; , , - html \. , PHP {}. PHP html; .

heredoc Here:

+1

, , - ?

<?php
    $variable = 'testing';
?>
<span class="label-<? echo variable ?>"><? echo variable ?></span>
+1

, $variable - , , :

echo "<span class=\"label-$variable\">${$variable}</span>";

${$variable} PHP, $variable. PHP .

, :

$itemName = 'pencil';
$variable = 'itemName';
echo "<span class=\"label-$variable\">${$variable}</span>";
//Prints: <span class="label-itemName">pencil</span>"
0

, ...

<?php
    $variable = 'testing';
    echo '<span class="label-[' . $variable . ']">[' . $variable . ']</span>;
?>
0

- .

<?PHP
    $variable = 'testing';
    echo "<span class=\"label-$variable\">$variable</span>";
?>

. , .

<?PHP
    $variable = 'testing';
    echo '<span class="label-' . $variable . '">' . $variable . '</span>';
?>

.

0
<?php
$variable = 'testing';
echo '<span class="label-'.$variable.'">'.$variable.'</span>';
?>
0

:

<?php
    $variable = 'testing';
    echo "<span class='label-$variable'>$variable</span>";
?>

, . , , , . .

:

<?php 
    $max_image_with = 1000;
    echo "<div style='width:${max_image_width}px;'>";
?>

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

0
source

PHP code ... It works

<?php 
$st_a = '<i class="fa fa-check-circle" ></i> Active'; 
?>

Display in HTML

<h1> Sample Heading <?php echo $st_a; ?></h1>
0
source

All Articles