问题
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;
Thanks much for your help.
回答1:
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.
回答2:
Try making an array:
$blurb = array();
$blurb[78] = "Lorem ipsum";
echo $blurb[$id];
回答3:
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
回答4:
This is your answer:
echo '$blurb_'.$id;
Still, an associative array is the way to go.
回答5:
Try
<?php
$blurb_78 = 'Lorem ipsum dolor.';
$id = 78;
echo ${'blurb_'.$id};
?>
回答6:
This should work:
echo ${'blurb_'.$id}
回答7:
echo ${'blurb_'.$id};
Demo
Read more on the variable variables article.
来源:https://stackoverflow.com/questions/7648688/php-concatenating-two-variables