PHP - Concatenating two variables

浪子不回头ぞ 提交于 2019-12-08 01:01:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!