I'm trying to write some code that will build a form more or less automatically. I start by defining a generic <input> tag like this:

$x =  "<input type='text' name='$name' value='$value' />"

Later on I have code that sets the $name and $value variables like this:

$name="ClientID"; 
$value = 12324;

The idea is to build the HTML <input> tag using the values of the variables so that I can echo like this:

echo $x;

and get something like this:

<input type='text' name='ClientID' value='1234' />

I've tried about every double quote, single quote, curly bracket variation I can think of but no luck. Usually I just get:

<input type='text' name='' value='' /> 

Any ideas?

    You need to assign the variables their values first, then define the string that uses them. If for some reason this is not feasible, then you'll need to use some sort of template-like system where you use a place-holder in the string that you then replace with the desired value via a replacement function such as [man]str_replace/man, or possibly even [man]sprintf/man.

    // place-holder:
    $string = "This is a <{value1}>.";
    $val1 = "test";
    echo str_replace('<{value1}>', $test, $string);
    
    // printf:
    $string = "This is a %s.";
    $val1 = "test";
    printf($string, $val1);
    

      Thanks for the quick reply. It looks like I'm going to have to go with the template approach.

        Write a Reply...