tags:

views:

65

answers:

6

I'm not sure if I asked the question correctly.

I have some code I am trying to embed. For instance:

$menuPopup ='<IMG SRC="' . $someVariable . '">';

Later on, I have the a few product variables:

$someProduct1 ='image1.jpg';
$someProduct2 ='image2.jpg';

Later on, want to display the $menuPopup, using a src from $someProduct1, or $someProduct2.

//Pseudo Code

$menuPopup ( $someProduct1);

Anyway to do that?

+3  A: 

create a function which takes one argument and returns

function gen( $arg ) {
    return '<img src="' . $arg . '">';
}
Mimisbrunnr
+3  A: 

Looks like you should make a function. Something like this:

function menuPopup($image) {
    return '<img src="'.$image.'">';
}

Then call it later on:

menuPopup($someProduct1);
Daniel Bingham
drr... just like Javascript! thanks
Jared
And many, many other languages - with variations. Keep learning new languages and you'll soon find that they are all simply variations on a theme. And there are only a few different themes. If you can do something in one there's a good chance you can do it in another and it'll look pretty similar.
Daniel Bingham
+4  A: 

Another good option is to use sprintf():

$template = '<img src="%s"/>';

echo sprintf($template, $someProduct1);  // => <img src="image1.jpg"/>
echo sprintf($template, $someProduct2);  // => <img src="image2.jpg"/>
Jordan
Thats a good solution too. Thanks!
Jared
A: 

You can create a dummy class taht can transfoprm itself into a string on demand, if you don't want a function:

class ImgTag {
    public $src;
    public __toString() {
        return '<img src="' . $this->src . '"/>';
    }
}

$img = new ImgTag();
$img->src = 'happy.gif';
# __toString() will be called whenever a string would be expected instead of
# this object. All of the below work:
echo $img;
echo implode(' ', array($mg, $img));
strpos($img, 'img');
soulmerge
A: 
*** PHP ***

function imageTpl( $src='' ){
    return (!empty($src)) ? sprintf('<img src="%s" />', $src) : '';
}

$product1_thumb = imageTpl( 'images/product1_thumb.jpg' );

*** HTML ***

<?=$product1_thumb?>
Kindred
A: 
function return_image_sourse( $src ) 
{
    return '<img src="' . $src . '">';
}
///////////
$new_src = "something";
return_image_sourse( $new_src ) ;
Syom