• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

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.

  • 2
Reply Report

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>
  • 1
Reply Report

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>
  • 1
Reply Report

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>"
  • 0
Reply Report

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.

  • 0
Reply Report

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

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions