Nishant Garg Nishant Garg - 1 month ago 15
HTML Question

Why echo's output needs to be separately put in quotes for placeholder's string in input field?

I am generating a string for placeholder dynamically via PHP echo function & if I don't put quotes around PHP tags the output only takes the first word of the string. Why so?

  1. placeholder=<?php echo "Hello World"?>

    It outputs only Hello in input field

  2. placeholder="<?php echo "Hello World"?>"
    (Note quotes around PHP tags)

    It outputs Hello World in input field.

Same also happens for value attribute of the input field.

Answer Source

This is due to how the placeholder attribute is constructed.

You should conform to the standard of putting your value in between quotation marks (see this example as proof).

It requires the values to be put in between quotations if you wish to add spaces to your value.

So imagine when you do this:

<input type="text" placeholder=<?php echo "Hello World"?> >

You are returning this in the HTML file:

<input type="text" placeholder=Hello World >

It reads 'Hello' as it's value and then HTML sees 'World' as another attribute. This is why you need to put your value in between quotes to signify that the entire string is apart of the placeholder attribute.