cusejuice
cusejuice

Reputation: 10691

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>

Upvotes: 10

Views: 49188

Answers (10)

Miguel Garc&#237;a
Miguel Garc&#237;a

Reputation: 13

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>

Upvotes: 1

Vijay Deva
Vijay Deva

Reputation: 21

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>

Upvotes: 0

metatron
metatron

Reputation: 1009

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

Upvotes: 0

Desert_king
Desert_king

Reputation: 96

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

Upvotes: 0

Tynach
Tynach

Reputation: 182

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>

Upvotes: 1

Brandon Anzaldi
Brandon Anzaldi

Reputation: 7270

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.

Upvotes: 0

Valera Leontyev
Valera Leontyev

Reputation: 1181

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

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

Upvotes: 0

Kip
Kip

Reputation: 109503

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

Upvotes: 0

Julio
Julio

Reputation: 2290

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

Should be

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

Upvotes: 12

Kermit
Kermit

Reputation: 34063

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.

Upvotes: 2

Related Questions