10

I know this is a basic php question, but I'm having trouble showing variables inside an echo of HTML. Below is what I'm trying to achieve.

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

should output:

<span class="label-testing">testing</span>
1

10 Answers 10

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

Should be

<?php
    $variable = 'testing';
    echo '<span class="label-' . $variable .'">' . $variable . '</span>';
?>
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Additionally, you can do it inline by escaping the double quotes:

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

Using double quotes also allows for special characters such as \n and \t.

The documentation is your friend.

Comments

1

I know this is an old question, but what's wrong with something like this?

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

Comments

1

One of the great features of php is the ability to be called and quit using php at any time during html. Here is what I'd do:

<?php
   // FIRST WRITE THE PHP PART 
   $variable = 'testing'; 
   // THEN START USE HTML 
   // (call it when it's needed later)
?>
<span class="label-testing"><?php echo $variable; ?></span>

Comments

0

If you are saying that $variable is the name of the variable you wish to print, you want this:

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

The ${$variable} syntax tells PHP to print the variable named $variable. PHP calls these variable variables.

So for example:

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

Comments

0

It's TOO basic question to be asked here...

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

Comments

0

Something like this should work.

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

The type of quotes on the echo are very important though. If you'd rather use single quotes on the echo, it'd be more like this.

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

This should allow you to echo back the variable.

Comments

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

Comments

0

This works too and is much sweeter:

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

Order of double and single quote matters, double quotes on the outside. Be careful if you use this syntax, if the variable name is followed by normal characters things might go horribly wrong. Curly braces around variable names are required then.

Example:

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

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

Comments

0

PHP Code... It's Working

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

To Display in HTML

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.