2

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
  • The phrasing is creating some confusion: do you want the output to be "$blurb_78" or "Lorem ipsum dolor."? I answered for the former; many for the latter. Commented Oct 4, 2011 at 13:41

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try making an array:

$blurb = array();

$blurb[78] = "Lorem ipsum";

echo $blurb[$id];

2 Comments

OP wanted to output the vas's name.
... And I was wrong. I'm going to upvote a random other question of yours, since I downvoted this more than 7 minutes ago.
0

You should use an associative array instead of variables in your case.

Check this article in PHP official documentation:

Comments

0

This is your answer:

 echo '$blurb_'.$id;

Still, an associative array is the way to go.

Comments

0

Try

<?php
$blurb_78 = 'Lorem ipsum dolor.';
$id = 78;
echo ${'blurb_'.$id};
?>

Comments

0
echo ${'blurb_'.$id};

Demo

Read more on the variable variables article.

3 Comments

Not really. Op wanted to output the string "$blurb_78" with the "78" part given as a variable.
@cbrandolino Huh? What is mine doing? I have even linked to a demo page so you can see how it's working. Edit: I see what you meant. I don't think OP was that PHP illiterate.
It's echoing the var's value, while I thought OP wanted its name to be displayed.
0

This should work for concatenating two variables:

echo ${'blurb_'.$id}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.