tags:

views:

82

answers:

6

For sake of performing less database queries and for clarity of code, I'd like include a yet to be defined variable inside a string. Later in the page, the variable will be declared and the string printed and evaluated. How do i do this?

$str="This $variable is delicious";

$array=array("Apple","Pineapple","Strawberry");

foreach($array as $variable)
{
  print "$str";
}
+13  A: 

You can use printf() (or sprintf() if you don't want to echo it):

$str = 'This %s is delicious';

foreach ($array as $variable) {
    printf($str, $variable);
}
NullUserException
+2 . . . . . . .
Peter Ajtai
This is perfect. Thanks very much!
matt
Or sprintf() if you don't actually want to echo it. +1
Mchl
@Mchl Thanks; incorporated into the answer
NullUserException
or vsprintf() if you want to add arguments dynamically (for the i8n you most likely would) :-P
Col. Shrapnel
A: 
$str='This $variable is delicious'; // so no variable interpolation is performed

$array=array("Apple","Pineapple","Strawberry");

foreach($array as $variable)
{
  // Warning! This is a very bad idea!
  // Using eval or system might create vulnerabilities!
  eval('$str="' . $str . '";');
  print $str;
}
coding.mof
This is a bad idea indeed. Not worth downvoting though, because it's noted on the answer it's a bad idea.
NullUserException
Therefore it's sayd in the listing..
coding.mof
A: 

Use str_replace.

For example:

$str = "This is [VARIABLE] is delicious";
$array = array("Apple", "Pineapple", "Strawberry");

foreach($array as $variable)
{
    print str_replace('[VARIABLE]', $variable, $str);
}
captaintokyo
My solution will work, but NullUserException's is better
captaintokyo
actually, yours is better, because it's often needed to change variables' positions for different languages: in english it's "close file" ([verb] [noun]) in german "datei schliessen" ([noun] [verb]).
stereofrog
@stereofrog: It's possible to swap order of variables in (v|s)printf(). See example #3 here: http://www.php.net/manual/en/function.sprintf.php
Mchl
@Mchl, sure, but `hello %2$s` is far less readable than `hello [name]`
stereofrog
A: 

Why don't you just do:

$array=array("Apple","Pineapple","Strawberry");

foreach($array as $variable) {
    print "This $variable is delicious";
}
dannybolabo
That would work. But this isn't the actual code I'm working with. I just used it for the purpose of the example.
matt
@matt that's bad of you
Col. Shrapnel
A: 

I think you need php's sprintf function

http://php.net/manual/en/function.sprintf.php

or it can also be done using str_replace

http://in.php.net/manual/en/function.str-replace.php

naiquevin
A: 

You are probably doing wrong way.
Learn to use templates and you will never need such odd things.
Just divide your code into 2 parts:

  • getting all required information
  • displaying a regular page or an error page

you will find that all your code become extremely neat and reusable

Col. Shrapnel
The OP might be using this for i8n
NullUserException
@Null good shoot. I should've been think of it.
Col. Shrapnel