1

Is there a way to print php variables inside a HTML tag?

Like this:

<?php
for ($e = 1; $e <= 30; $e++) {
  for ($i = 1; $i <= 40; $i++) {
    print ('<div id="map"><a>$i</a></div>');  // <----I'm adding the divs here, but I want to add a text inside each of them
  }
}
?>

6 Answers 6

4

Your call:

echo '<a href="'.$href.'">'.$title.'</a>';

or

echo "<a href='$href'>$title</a>";

or

<a href="<?= $href ?>"><?= $title ?></a>

I prefer the first one, because I think it is the most readable. But it is up to you.

Using your example:

print ('<div id="map"><a>'.$i.'</a></div>'); 

or

print ("<div id=\"map\"><a>$i</a></div>"); // note the double quotes to allow PHP to parse the string

or

<div id="map"><a><?= $i ?></a></div> // but you really should do this!

To better clarify you SHOULD NEVER rely on short tags (the last option). Every time you do that god will kill a kitten and abuse a unicorn.

6
  • 1
    with short_open_tag = "1" for this last one. Jan 9, 2012 at 21:05
  • @LucLaverdure what do you mean? Did I screw something up? :P
    – PeeHaa
    Jan 9, 2012 at 21:08
  • 1
    Isn't it faster to do echo '<a href="', $href, '">', $title, '</a>';? I don't remember, but I thought I read an article on that.
    – animuson
    Jan 9, 2012 at 21:08
  • @animuson actually it is faster, but it is by convention that I never use it. And the performance gain is really small. Nice comment though!
    – PeeHaa
    Jan 9, 2012 at 21:10
  • 1
    No you didn't mess up anything, everything is fine, but you need short_open_tag = "1" in php settings or it will fail to load <? ?> instead of <?php ?> ;) Jan 9, 2012 at 21:18
2

In this case take the variable out of the string and concatenate it inside.

print ('<div id="map"><a>'.$i.'</a></div>'); 

This is pretty basic stuff so I suggest you go through some tutorials or read the manual again.

1
  • it did the trick! I don't use php often, so this kind of thigs are totally out of my mind ! thanks
    – Marco
    Jan 9, 2012 at 21:07
0

I'm not sure what text you want to add there, you can have any other variables in there, same how you're printing the $i .. except that single quotes do not evaluate variables inside the string, only double quotes do ..

so:

$foo = "bar";
print "$foo"  => "bar"
print '$foo'  => "$foo"
1
  • Thanks guys for all the support!
    – Marco
    Jan 9, 2012 at 21:21
0

You're using single quotes to output your variable. using double quotes may work instead, but I prefer using string concatenation(. operator)

print ("<div id=\"map\"><a>$i</a></div>");

Or

print ('<div id="map"><a>' . $i . '</a></div>');
0

Use " instead of ' when you want to use variables in a string.

0
<a href='<?php echo "http://www.stackoverflow.com"; ?>' target='_blank'>test</a> (Syntax maybe off)

EDIT

<?php
for ($e = 1; $e <= 30; $e++) {
    for ($i = 1; $i <= 40; $i++) {
        print ('<div id="map"><a>'. $i .'</a></div>');
    }
}
?>

Note the '. $i .' part.

2
  • Why are you concatenating JavaScript style? Jan 9, 2012 at 21:07
  • Ah thx, Code is updated. :) Because I use ~5 different languages everyday and I'm easily confused :P Jan 9, 2012 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.