0

I am very new to PHP and can't seem to get the following to work. Any help would be appreciated. I am trying to echo a HTML input field and set the value to a variable.

echo "<input type='hidden' name='item_name' value='<?php echo $one.$two; ?>'>" ;

What's wrong with this? When I inspect it on the page it does not show the value of the variable.

2
  • 2
    echo "<input type='hidden' name='item_name' value='" . $one.$two . "'>" ; Commented May 26, 2016 at 19:27
  • 1
    The main problem is that you're already executing PHP when you do the first echo, so opening another <?php within the string you're echoing doesn't make sense. Commented May 26, 2016 at 19:32

2 Answers 2

3

You must do like this

echo "<input type='hidden' name='item_name' value='".$one.$two."'>" ;

Strings and variables are connected with .

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

2 Comments

Thanks for this I tried everything this was driving me crazy even after your answer I could not get it to work. I did not know that string concatenation required the dots in php. Thanks for your help. It turned out the variables were empty anyway that is why I could not see them.
What we are doing here is separating html as string and attaching it to the PHP variables.
1
 echo '
  <input type="hidden" name="item_name" value="'.$one.$two.'">';

I like to echo html using single quotes so input values can use doubles.

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.