PHP echo variable and html together

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>

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

Should be

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

Solution 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.

Solution 3

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>

Solution 4

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>

Solution 5

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>"

Solution 6

It’s TOO basic question to be asked here…

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

Solution 7

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.

Solution 8

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

Solution 9

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

Solution 10

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>

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply