PHP - Concatenating two variables
I'm a newbie stumped on the following. If I'm including an external file on a page that contains the following variable:
$blurb_78 = "Lorem ipsum dolor.";
How can I echo $blurb_78 on the local page?
(where the 78
part is a generated article ID set to a variable labeled, $id
)
The following doesn't work:
echo $blurb_.$id;
Th开发者_StackOverflow中文版anks much for your help.
I think you mean a variable variable name like it is mentioned at the Variable variables page on the PHP site. In your case this should work fine:
echo ${'blurb_'.$id};
But I highly doubt your approach on this one.
Try making an array:
$blurb = array();
$blurb[78] = "Lorem ipsum";
echo $blurb[$id];
You should use an associative array instead of variables in your case.
Check this article in PHP official documentation:
- http://php.net/manual/es/language.types.array.php
This is your answer:
echo '$blurb_'.$id;
Still, an associative array is the way to go.
Try
<?php
$blurb_78 = 'Lorem ipsum dolor.';
$id = 78;
echo ${'blurb_'.$id};
?>
echo ${'blurb_'.$id};
Demo
Read more on the variable variables article.
This should work for concatenating two variables:
echo ${'blurb_'.$id}
精彩评论